C# for-loop performance estimates: i++ vs. ++i implementation: which is faster? -
my question relates performance comparison (numeric estimates) pertinent 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";} (int i=0; i<10000; ++i;){string _s="a";} (int i=10000; i>0; i--;){string _s="a";} (int i=10000; i>; --i;){string _s="a";}
question: of following implementations provide better performance (execution time) in generic for-loop implemented in c# 4.0 or c# 5.0?
note 1: string _s="a"; sample operation, potentially omitted testing purpose.
note 2: far, per discussion on (is there performance difference between i++ , ++i in c?) seems ++i runs faster i++ in c++.
the optimizer should know pre-incremented value of i
never used, , therefore should same (simply increment i
).
the reason i++
may slower ++i
if compiler has save off old, pre-incremented value before incrementing i
. however, given low register pressure, should trivial 1 additional "move" instruction.
if really care, suggest benchmark test cases. stopwatch
provides enough functionality pretty estimation.
also, looking @ generated il show if there difference @ all.
Comments
Post a Comment