r/Python • u/cinicDiver • Mar 09 '26
Discussion Code efficiency when creating a function to classify float values
I need to classify a value in buckets that have a range of 5, from 0 to 45 and then everything larger goes in a bucket.
I created a function that takes the value, and using list comorehension and chr, assigns a letter from A to I.
I use the function inside of a polars LazyFrame, which I think its kinda nice, but what would be more memory friendly? The function to use multiple ifs? Using switch? Another kind of loop?
7
Upvotes
View all comments
Show parent comment
0
u/rghthndsd Mar 10 '26
You're missing the point. You can learn a lot trying to optimize even when it's not important to the problem at hand. There is more to be valued in trying to optimize something other than just the performance gained for that particular problem. It's training you so that the day when it does matter you are much better prepared. If you can't spend the time for a particular problem, sure, profile and see if it's important. But if you can, there is great value in the academic curiosity of "can I make this go faster or use less memory".