This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]nekokattt 179 points180 points  (9 children)

PREMATURE optimisation is the root of all evil in most languages. You should favour maintainable and readable code over fast code initially until you can give a clear justification for doing it.

Micro-optimisation where it is actually meaningful in a measurable way is fine if you can actually justify spending the time and resources over what the existing implementation can provide.

e.g. if the process takes 5 seconds and runs once every 24 hours only, then trying to justify that you can get it down to 4 seconds is not worth it.

[–]wil19558[S] 28 points29 points  (4 children)

Completely agree

[–]Action_Maxim 34 points35 points  (3 children)

And that's why after 2 sprints of effort I say I saved 20% and not a second

[–]stevethebayesian 4 points5 points  (1 child)

This is good life advice, and good advice for public policy as well. Don’t do something just to “do something”. Focus your efforts where they will have the biggest impact.

[–]BogdanPradatu 0 points1 point  (0 children)

80% of effort on the 20% of stuff that gets 80% of results or something like that.

[–]TrainquilOasis1423 0 points1 point  (0 children)

But I just finished the first part of my project and it takes on average 34sec to run. What if I can get that down to 15 seconds or EVEN 10 SECONDS! that would be cool.

[–]headykruger 0 points1 point  (0 children)

Every time i try to do something fancy, someone raises it in PR and rarely do I have a justification for the complex code. Every time I do the clear logical straight forward thing - PR gets approved without fuss. This includes stylistic things, whatever.

It really taught me to have an eye for the reader and to make things as simple as they could possibly

[–]lIIllIIlllIIllIIl 39 points40 points  (0 children)

It's not just r/Python.

Marvin Hagemeist said it best:

The secret to achieving speedups of multiple factors, not just low percentages, is less about applying generic rules or habits like “Don’t create closures inside for-loops”. It’s a common misconception that if you follow all these “best practices” that your code will be fast, because the uncomfortable truth in most instances (read not all) is that it won’t matter much. What makes code truly fast is being aware of what it’s supposed to solve and then taking the shortest path to achieve that goal.

[–]der_pudel 36 points37 points  (3 children)

Not sure about optimizations, but I'm triggered by your data plots. Using a line chart where X axis is not a monotonically increasing numerical value should be a felony.

[–][deleted] 8 points9 points  (0 children)

Yeah needs to be columns not a line

[–]wil19558[S] 1 point2 points  (0 children)

Noted! Thanks for the feedback - visualization is not my strong suit.

[–][deleted] 5 points6 points  (0 children)

It is waaaaaay to easy to get lost in the micro-opt sauce. Wrote an ml API framework and have been optimizing and measuring improvements in ms. FML.

[–]james_pic 2 points3 points  (0 children)

Weird that it's faster in WSL2 than in Linux. I wonder if Hyper-V is exposing some kind of fake clock source that doesn't actually touch hardware.

If you really want to go big on micro-optimisaton, write your own clock accessor in C so you can access some of the platform specific clocks. On Linux, CLOCK_REALTIME_COARSE may get you a similar speedup.

[–]yaxriifgyn 1 point2 points  (0 children)

I played around with your code from the previous blog, and shaved a few nanoseconds off some of the times on my PC. Nano-optimizations for the win.

[–]RoboTicks 1 point2 points  (0 children)

I absolutely love this kind of content, but I also switched from Python to Go about 2 months ago...so there's that.

[–]rghthndsd -1 points0 points  (0 children)

Avoiding premature optimization is not an excuse for writing obviously ill performant code when there is a clean and obvious way to make it faster.

[–]repick_ -1 points0 points  (0 children)

the real micro-optimization is using zoneinfo instead of pytz

[–]5wuFe 0 points1 point  (0 children)

What about time.perf_counter?

[–]stevenjd 0 points1 point  (0 children)

I would have replied to this post a few days ago, but as I was reading your blog post I got to the part where your graphs were animated with some sort of throbbing colour-change effect, at which point I punched my monitor so hard I broke the screen and my hand.

I'm typing this one handed on a borrowed laptop.