[C#] What is the difference between these two loops? Is one faster?

Started by
5 comments, last by Fenrisulvur 14 years, 1 month ago
What is the difference between:

            for (int i = 0; i < 10; ++i)
            {
                Console.WriteLine(i);
                // Still does 0 to 9
            }
and:

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(i);
                // Still does 0 to 9
            }
Is one faster than the other when recompiled? Thanks.
Advertisement
No.

EDIT: In order to be a little less useless:

The differences between the prefix and postfix versions of the increment and decrement operators are only noticeable when used as part of a larger expression.

In fact, if you compile that program of yours and look at it in reflector, you'll see that the two loops produce identical IL. When in C# mode, reflector will show them both as postfix, since that's the more commonly used version. In older C code, there was sometimes a slight difference in that an extra temporary would be used with the postfix version, but modern compilers don't have this problem.

On top of all that, C# doesn't let you overload the prefix and postfix versions separately, so it's not like you can get differing behavior if some knucklehead decides to be cheeky.
Mike Popoloski | Journal | SlimDX
Since the ++i and i++ are both on their own, outside another expression, they will both compile to the same code. So, as Mike already said so succinctly, there is no difference [smile]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

there is a difference between ++i and i++ but for int there will be absolutely no detectable speed difference in your example. In this case the compiler will probably even detect that the two loops are identical and compile to the same code.

So what is the difference? ++i says 'increment i and give me the new value' while i++ says 'increment i but give me the value before incrementing.' It becomes more clear in code:
int pre( int i ) { // ++i    i += 1;    return i;}int post( int i ) { // i++    int tmp = i;    i += 1;    return tmp;}

However this difference is only important if A) you actually assign the value of the expression to something (e.i. int j = ++i;) or B) the object you increment are very costly to copy.

Back in the olden days of assembly and C, when compilers weren't so smart, programmers where more concerned by never wasting any cycles. Therefore older programmers will often use the ++i form out of habit. But nowadays it's a mostly obsolete micro-optimization when dealing with trivial types.
Despite the fact that everything in this thread is true, I find it useful to get in the habit of *always* using prefix increment unless you absolutely need the semantics of postfix. This is because sometimes it is faster, and so you might as well be using it as much as possible so you don't have to think about it.

Granted, in C# you'll encounter these types of differences much less often than in C++, where you can use ++ on objects of arbitrarily complex types, but it's certainly not a *bad* habit to always use prefix increment, so you might as well.
Quote:Original post by Promethium
Back in the olden days of assembly and C, when compilers weren't so smart, programmers where more concerned by never wasting any cycles. Therefore older programmers will often use the ++i form out of habit. But nowadays it's a mostly obsolete micro-optimization when dealing with trivial types.


Except that this isn't actually the history. i++ was more common in C because it was idiomatic; i.e. some people started doing it and it caught on, and people didn't really think about it very much. (I think it may have been the case that K&R published code written that way.) After all, there's a reason C++ is not named ++C. However, once C++ became popular and people started thinking seriously about operator overloading, it occurred to people that the prefix form will be at least as fast in general, and often faster. As a result, it became the rule of thumb to use the prefix form when you can and the postfix form when you have to (i.e. you use the return value). A no-brainer, really, since it can't hurt performance, could help performance, and doesn't take any more programmer effort (except possibly to break the old habit).
Quote:Original post by Zahlman
doesn't take any more programmer effort (except possibly to break the old habit).

I consider this too much effort.

Also, I absolutely hate seeing the ++ operator in any statement larger than the following:

i++;

This topic is closed to new replies.

Advertisement