C# Optimizing Compiler

Let me share nice thing that I noticed about C# compilation model. What I did was, I created a small program to print an Int32 array to console. Then I went into IL and checked out various function calls that C# compiler has made. There was one nice thing that I noticed. C# compiler sometimes optimizes your code by itself. Let me show you,

using System;

namespace Optimization
{

class Program

{
static void Main(string[] args)
{
Int32[] arr ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,11 };
for (Int32 i = 0; i<=arr.Length-1;i++) {
Console.WriteLine(arr[i]);
}
Console.ReadLine();

}

}

}

This is the code that I wrote to print a simple Int32 array. Below is the IL generated by this code snippet.


Compiler stores the array length in line IL_0000. The you can see there is no call to Array.Length property [internally it will call the method get_Length()] which I have used in For loop. Only calls that C# compiler added were to Initialize array, WriteLine() and ReadLine(). C# compiler have optimized our code internally so that there is no need to call Length with every loop iteration. It uses the length that is stored in the beginning to provide a performance boost. So, if you are doing such thing in your code, don’t complicate things by declaring a variable just before the for loop just to store array length and use that variable in loop condition. You will be doing that to improve your application’s performance but doing such heroics is not needed in this scenario.

I have just started to read about ASP.net internals and currently I am dating HTTPSys driver :). I hope to get this done by November 2nd week.
Currently listening To: SHAGGY Feat. AKON – What’s Love
nJoy CoDiNg

Advertisements

7 comments

  1. When responding to questions on an interview, stay as straight and succinct as you can. The job interviewer will know should you not fully grasp one thing so try not to surpass across the bush. Obvious answers can have you are aware what you really are referring to and not producing up solutions on the spot. [url=http://www.x21w12w21.info]Heukuk8ily[/url]

    Like

  2. If you wish to get whiter the teeth, take in food items that happen to be loaded with fiber. Although food items are more inclined to mark your teeth, meals that are loaded with fiber, like carrots, spinach, celery, and broccoli exfoliate your teeth before blemish substances can affix to your teeth and stain them. [url=http://www.ss12w12ws.info]Pro232rgram[/url]

    Like

  3. At bed time, raise your head making use of special pillows. This may let your air passages to remain open as you may sleep at night, and definately will minimize obstructive sleep apnea. Just ensure that your neck area and again are comfortable so that you are certainly not leading to any soreness in these regions while you are looking to assist your sleep apnea. [url=http://www.x21w12w21.info]Ca45yhhe[/url]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s