All over the web, code samples have for
loops which look like this:
for(int i = 0; i < 5; i++)
while I used the following format:
for(int i = 0; i != 5; ++i)
I do this because I believe it to be more efficient, but does this really matter in most cases?
I think in the end it boils down to personal preference.
I like the idea of
over
due to there being a chance of the value of i jumping past 5 for some reason. I know most times the chances on that happening are slim, but I think in the end its good practice.
If your index were not an
int
, but instead (say) a C++ class, then it would be possible for the second example to be more efficient.However, as written, your belief that the second form is more efficient is simply incorrect. Any decent compiler will have excellent codegen idioms for a simple for loop, and will produce high-quality code for either example. More to the point:
In a for loop that's doing heavy performance-critical computation, the index arithmetic will be a nearly negligible portion of the overall load.
If your for loop is performance-critical and not doing heavy computation such that the index arithmetic actually matters, you should almost certainly be restructuring your code to do more work in each pass of the loop.
Numeric literals sprinkled in your code? For shame...
Getting back on track, Donald Knuth once said
So, it really boils down to which is easier to parse
So... taking into account both of the above, which of the following is easier for a programmer to parse?
Edit: I'm aware that arrays in C# implement the System.Collections.IList interface, but that's not necessarily true in other languages.
When I first started programming in C, I used the
++i
form in for loops simply because the C compiler I was using at the time did not do much optimization and would generate slightly more efficient code in that case.Now I use the
++i
form because it reads as "increment i", whereasi++
reads as "i is incremented" and any English teacher will tell you to avoid the passive voice.The bottom line is do whatever seems more readable to you.
I agree with what's been said about readability - it's important to have code that's easy for a maintainer to read, although you'd hope that whoever that is would understand both pre- and post-increments.
That said, I thought that I'd run a simple test, and get some solid data about which of the four loops runs fastest. I'm on an average spec computer, compiling with javac 1.7.0.
My program makes a for loop, iterating 2,000,000 time over nothing (so as not to swamp the interesting data with how long it takes to do whatever is in the for loop). It use all four types proposed above, and times the results, repeating 1000 times to get an average.
The actual code is:
Sorry if I've copied that in wrong!
The results supprised me - testing
i < maxValue
took about 1.39ms per loop, whether using pre- or post-increments, buti != maxValue
took 1.05ms. That's a that's either a 24.5% saving or a 32.5% loss of time, depending on how you look at it.Granted, how long it takes a for loop to run probably isn't your bottleneck, but this is the kind of optimisation that it's useful to know about, for the rare occasion when you need it.
I think I'll still stick to testing for less than, though!
Edit
I've tested decrementing i as well, and found that this doesn't really have an effect on th time it takes -
for (int i = 2000000; i != 0; i--)
andfor (int i = 0; i != 2000000; i++)
both take the same length of time, as dofor (int i = 2000000; i > 0; i--)
andfor (int i = 0; i < 2000000; i++)
.The second is less readable, I think (if only because the "standard" practice seems to be the former).