Standard Loop Progression

This tip submitted by Dazzle on 2005-08-07 15:12:14. It has been viewed 44574 times.
Rating of 6.3 with 270 votes



Most people using for-loops tend todo things a way that I find to just be annoying.

For example.

for(x = 0; x < 10; x++)

This method when you break it down into assembly, takes 3 extra movements.

There is a simple fix for this, and it will optimize your loop, ever so slightly.

for(x = 0; x < 10; ++x)

Really basic? The same goes for working with iterators, and in fact, this is the place you'll want to do this the most.

Example.

for(g = g_list.begin(); g != g_list.end(); ++g)

I use ++g instead of g++ because ++g cuts out those same, 3 movements, it can save milliseconds on your code rotations, for those using for loops like this, this is a much faster way to parse through your data.

Of course, for maximum speedup, you want to save the end iterator in a variable rather than invoking the end() every time!



More tips

Help your fellow programmers! Add a tip!