C# Optimizations Via Caching – Part 2

On March 22, 2009, in C#, Programming, by Akash Kava

I decided to write part 2 of optimization, with C# Project, to actually test the performance for accessing NameValueCollection and Property access.

I had lot of comments by extra smart people quoting regarding my previous post at "C# Optimizations Via Caching" . That all methods posted do not do any significant impact on performance, I wonder people just have habit of posting any thing without even thinking once.

So I decided to write a piece of code to actually demonstrate the optimization impact with statistics. In this Visual Studio 2008 C# Console Project, I wrote a simple code to access an application config value directly for 10,000 times in a loop and then to access it via statically cached variable. And in second part, I implemented accessing property and accessing a local variable nearly 10 million times to detect noticeable effect.

The tests are as follow,

Lets look at the test code to access config values, also please note that config values are stored in NameValueCollection, which is same object used in Request, Response and Session objects in web request.

// Configuration Access Test
Console.WriteLine("Testing Direct Config Access");

Test(1000000, () => {
     string v = System.Configuration.ConfigurationManager.AppSettings["Key"];
});

Console.WriteLine("Testing Cached Access");
Test(1000000, () => {
     string v = SettingVal;
});

Following is the result, on Intel Centrino Duo with 1GB of RAM, this shows that the code with cached access in static variable is 100 times faster.

Testing Direct Config Access

Time Difference 1125 MilSecs

Testing Cached Access

Time Difference 15.625 MilSecs

So now next time you code, remember it to not access frequently in loops.

Lets talk about property access,

// Property in Loop Test
int pc = 10000000;
Console.WriteLine("Testing Direct Property Access");
Test(() => {
    Person p1 = new Person();
    for (int i = 0; i < pc; i++)
    {
        string v = p1.Name;
    }
});

Console.WriteLine("Testing Local Variable Access");
Test(() => {
    Person p1 = new Person();
    string t = p1.Name;
    for (int i = 0; i < pc; i++)
    {
        string v = t;
    }
});

In order to detect any noticeable difference, we have to loop for about 10 million times, however, in any usual code, you will be accessing 100s of properties, so it may make a good difference when you write code, which executes million times parallel.

Testing Direct Property Access

Time Difference 140.625 MilSecs

Testing Local Variable Access

Time Difference 46.875 MilSecs

Now based on the amount of free memory, and cpu speed, you might have to configure the number of iterations up and down to get a good noticeable effect of two different ways.

I can clearly see the difference, remember on normal piece of code, you will be accessing over 10 to 100s of properties again and again.

Click on Caching Stats Visual Studio Project to download and test by yourself.

Share
Tagged with:  

One Response to C# Optimizations Via Caching – Part 2

  1. Hi, cool post. I have been wondering about this topic,so thanks for writing.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>