[Solved] Which for loop is more efficient in C++? [closed]


Any decent compiler will not penalize you for which ever version you use.
As you can see, all the loops boil down to the same code, with maybe one instruction reordered.

Go for the one that is most readable and clearly conveys what you want to do.

If you are interested in what happens at no optimizations, here you go:

Example A

Example B

Example C

As you can see, A and B produce exactly the same code even at no optimizations, and C has actually more instructions (and in my opinion, is far less readable)!

The code has been analyzed under G++ 5.3, the results can vary between different compilers. The main point is: it’s a micro (nano?) optimization.

2

solved Which for loop is more efficient in C++? [closed]