Should we still be optimizing “in the small”?

后端 未结 22 2140
旧时难觅i
旧时难觅i 2020-12-05 11:32

I was changing my for loop to increment using ++i instead of i++ and got to thinking, is this really necessary anymore? Surely today\'s compilers

22条回答
  •  一个人的身影
    2020-12-05 12:05

    Only if you know for certain that they're relevant. This means either you've investigated this issue before on your particular compiler or you've already done the following:

    1. produced functional code
    2. profiled that code
    3. identified bottlenecks
    4. simplified design to eliminate bottlenecks
    5. chosen algorithms that minimize calls into bottlenecks

    If you've done all of these things then often the best thing to do is get your compiler to emit something lower level that you can examine yourself (like assembly) and make specific judgments based on that. In my experience every compiler is a little different. Sometimes an optimization on one causes another to produce larger less efficient code.

    If you haven't already done these things then I'd call it premature optimization and I'd recommend against it. Optimizing before doing these things gives rewards which are disproportionately small when compared to the cost involved.

提交回复
热议问题