Should we still be optimizing “in the small”?

后端 未结 22 2143
旧时难觅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 11:42

    • I don't generally optimize lower than the O(f(n)) complexity unless I'm writing on an embedded device.

    • For typical g++/Visual Studio work, I presume that the basic optimizations are going to be reliably made(at least when optimization is requested). For less mature compilers, that assumption is presumably not valid.

    • If I was doing heavy maths work on streams of data, I'd check the compiler's ability to emit SIMD instructions.

    • I'd rather tune my code around different algorithms than a specific version of a specific compiler. Algorithms will stand the test of multiple processors/compilers, whereas if you tune for the 2008 Visual C++(first release) version, your optimizations may not even work next year.

    • Certain optimization tricks that are very reasonable in older computers prove to have issues today. E.g., the ++/++ operators were designed around an older architecture that had an increment instruction that was very fast. Today, if you do something like

      for(int i = 0; i < top; i+=1)

      I would presume that the compiler would optimize i+=1 into an inc instruction(if the CPU had it).

    • The classic advice is to optimize top-down.

提交回复
热议问题