ASP.NET Hosting

Weighting optimizations' worth

Scott and Victor had a little discussion about getting the value inside the loop vs outside the loop.  Now common sense would dictate to me that outside the loop would of course be faster.  So, for my own amusement I threw together a little test.  I simply ran their code and tried to figure out which one was faster.  Going through a 12 item array, declaring with the loop (i < array.Length) actually was 2 seconds faster than getting it outside the loop.  Of course, to get a 2 second difference I had to run each chunk of code 500,000,000 times.  The difference may have simply have been the overhead of declaring a variable to store the length.  I'm not too sure, I didn't dig into the IL.

My point is that do you feel that this type of micro optimatization is worth it to get a 0.000000004s boast?  I say go with the most readable.  Very rarely do these micro optimizations have any impact big impact, when improvements in the architecture of your apps is where you really start to see some improvements.  Macro optimizations. 

Oh, I stole this idea from Brad.

btw, the better performing code changes in debug vs release.  outside the loop wins in debug mode (no optimizations), while with the loop wins in release.

[Phil Scott]

Phil's remark makes a lot of sense.
Time is valuable and must be... optimized :-) Spend it on things that impact your applications the most.

No Comments