0

My question relates to performance comparison (numeric estimates) pertinent to the following sample cases implementing for-loop in C# 4.0 and/or C# 5.0 in 4 different manners:

for (int i=0; i<10000; i++;){string _s="a";}
for (int i=0; i<10000; ++i;){string _s="a";}
for (int i=10000; i>0; i--;){string _s="a";}
for (int i=10000; i>; --i;){string _s="a";}

Question: Which of the following implementations will provide better performance (execution time) in generic for-loop implemented in C# 4.0 or C# 5.0?

Note 1: string _s="a"; is just a sample operation, potentially could be omitted for testing purpose.

Note 2: so far, as per discussion on (Is there a performance difference between i++ and ++i in C?) it seems like ++i runs faster than I++ in C++.

Community
  • 1
  • 1
Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
  • 1
    I'm sure you understand that 1) you have to be doing *almost nothing* inside the loop (a lot less than string =), and 2) this loop has to be responsible for 10% or more of total execution time, for the difference between ++i and i++ to matter enough to care. – Mike Dunlavey May 13 '13 at 00:05
  • @MikeDunlavey: Thanks for your response. I perfectly understand that, so I put just a single utmost simple assignment statement in operational brackets. Therefore, all that increment/decrement part (i++, i--, etc.) will contribute significantly (percent-wise) to overall execution time, and the difference is expected to be detectable/quantifiable. – Alexander Bell May 13 '13 at 00:12
  • 7
    It makes no difference, the side effect of the expression doesn't get used. The jitter has no trouble seeing that. Why you haven't documented your question with your measurements is hard to guess. – Hans Passant May 13 '13 at 01:10
  • 2
    See http://stackoverflow.com/questions/467322/is-there-any-performance-difference-between-i-and-i-in-c/467944#467944. And if you have any doubts, why not take 5 minutes to write a little test program and find out for yourself? – Jim Mischel May 13 '13 at 13:31

1 Answers1

3

The optimizer should know that the pre-incremented value of i is never used, and therefore they should be the same (simply increment i).

The only reason i++ may be slower than ++i is if the compiler has to save off the old, pre-incremented value before incrementing i. However, given low register pressure, this should be as trivial as one additional "move" instruction.

If you really care, I would suggest you benchmark your test cases. Stopwatch provides enough functionality to get a pretty good estimation.

Also, looking at the generated IL will show you if there is any difference at all.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Thanks for your response, though it contradicts to outcome of the discussion I've mentioned (btw, that discussion is IMHO a bit overloaded with lengthy "theoretization" instead of providing clear verifiable numeric estimates). It seems like ++i can ran faster as per the info from that discussion thread (exerpt from their accepted answer (quote)"i++ could potentially be slower than ++i, since the old value of i might need to be saved for later use, but in practice all modern compilers will optimize this away." (end quote). Thanks and rgds, – Alexander Bell May 12 '13 at 23:40
  • If I weren't heading out shortly, I would write up the benchmark myself. But I challenge you to try it! – Jonathon Reinhart May 12 '13 at 23:41
  • And I'm not sure what I said that contradicts the other post. `i++` *may* be *slower* than `++i`. The accepted answer said the same thing as what I said in the first paragraph (if the pre-inc value isn't used, they're identical). – Jonathon Reinhart May 12 '13 at 23:42
  • Sorry, Jonathan, I was responding to your 1st line: "The optimizer should know that the pre-incremented value of i is never used, and therefore they should be the same (simply increment i)." So, are they the same or one is slower than other? It's kinda mutual exclusive. Thanks again, – Alexander Bell May 12 '13 at 23:45
  • I would agree, that the best way is probably to develop some benchmark app using that StopWatch and get the accurate estimates. I've done it recently for string concatenation algos comparison, and probably will re-use that settings. Rgds, – Alexander Bell May 12 '13 at 23:49
  • 4
    @AlexBell `if (!pre_increment_value.is_used && optimizer!=crap) { speed(i++) == speed(++i) } else { speed(i++) < speed(++i) }` – Jonathon Reinhart May 13 '13 at 01:10
  • 1
    @JonathonReinhart you could not have said it better :) – Lorenzo Dematté May 13 '13 at 12:22
  • I really regret that the question is closed: it relates to quintessentially important high-performance computation domain. Anyway, I would like to thank @JonathonReinhart for his insightful answer, accept and up-vote it. Just adding my own one line summary to the Question: in any possible scenario for-loop implementing ++i provides equal or better performance that i++. Best regards, – Alexander Bell May 17 '13 at 23:48