4
timourf
7y

Does any of you have the compulsion to micro-optimize every bit of code that you write? How do you deal with it?

I'm not just talking about algorithmic optimizations, but the real nitty gritty stuff. I'm talking about using bit fiddling to avoid if statements where speculative processors might make mispredictions. Anything that might make a program compile to fewer machine instructions or avoid extra stack frame overhead.

This all started a year ago when I took a systems programming course at my university, and started learning C and C++. But I find myself doing this in the wrong places. Who cares if this trivial program that I wrote runs in 1.2 or 0.6 seconds? My future employers won't care if my code is 10% more efficient when it takes four times as long to write.

It's gotten to the point that I can't bring myself to use languages like Python because I don't know how it's implemented under the hood and can't predict how the different ways I could write a function will affect performance. How do I bring myself to trust that the compilers (or interpreters) and the programmers that wrote them will be sufficiently optimal, and just move on? 😩

Comments
  • 1
    That's actually pretty easy, Just write your code and only optimize / refactor under 2 circumstances:

    1. The code needs to run faster

    2. You have phases or specific times scheduled to refactor your code.

    First functionality, then speed, then style. But you should use a linter of some kind tho.
  • 0
    Just stop and don't optimize until the end, once you have identified all the major bottlenecks, after that you may fiddle with it all you want because either way the work will be done.
  • 0
    @R5on11c The problem is, from my perspective, the code always needs to run faster. Your idea about scheduling time to rector and optimize is a good one, though.
    I'm not sure I understand how a linter would help in this situation, though. Care to elaborate?
  • 1
    @timourf understand and get away from premature optimisation. Learnt the hard way. If it's not needed then it's not worth it
Add Comment