Geeks With Blogs
John Conwell: aka Turbo Research in the visual exploration of data

I was reading this blog entry about optimizing too much and the author makes a very good point.  When he wanted to know which of two options was faster, he followed the number one rule of code optimization: He measured it.  This may sound obvious, but many people just skip this step.  They look at the code, and for what ever reason, make a judgment as to which one is faster.  But without measuring you will never know, and whats worse, you might even make your code slower!

Another thing I wanted to point out, that a lot of programmers don't really think about, is that C#, VB.net, FillInTheBlank.net are just abstractions around a stack based language called IL.  I'm sure most .Net programmers understand that their code gets compiled down to IL, but don't really understand what that means when thinking about performance.  I've done many code reviews in the past when the programmer changed something that was 10 lines of code down to just 3 lines of code in the name of optimization (without measuring I might add).  I mean, 10 lines of C# must run faster than 3 lines, right?  It depends.  You have to think about what the IL that is generated will do, and what and how many operations are taking place in those 10 vs 3 lines of code.  Many times taking a verbose block of code and cutting the lines of code down by half will can actually cause extra operations to be executed by the processor, as in the example posted by the above mentioned blog. 

I have found that the best way to understand whats really happening underneath the hood of you C# is by opening your assembly in ILDASM and looking at the IL that is generated.  It might take some time to understand the IL, but in the long run it will make you a better programmer.

Posted on Monday, October 31, 2005 5:59 AM Ramblings | Back to top


Comments on this post: What is your code really doing?

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © John Conwell | Powered by: GeeksWithBlogs.net