The Inquisitive Coder – Davy Brion's Blog

Trying to walk that thin line between intelligence and ignorance

Put Performance Concerns Into Perspective

Posted by Davy Brion on April 18th, 2009

There is a reported performance issue with NHibernate that i wanted to look into. The reported issue was related to retrieving objects through a generically typed List or through an IList reference.

The following code simulates the issue:

    class MyClass {}

 

    class Program

    {

        static void Main(string[] args)

        {

            List<MyClass> list = new List<MyClass>();

 

            Stopwatch stopwatch = Stopwatch.StartNew();

 

            for (int i = 0; i < 100000; i++)

            {

                list.Add(new MyClass());

            }

 

            stopwatch.Stop();

 

            Console.WriteLine("Elapsed ms: " + stopwatch.ElapsedMilliseconds);

 

            IList iList = new List<MyClass>();

 

            stopwatch = Stopwatch.StartNew();

 

            for (int i = 0; i < 100000; i++)

            {

                iList.Add(new MyClass());

            }

 

            stopwatch.Stop();

 

            Console.WriteLine("Elapsed ms: " + stopwatch.ElapsedMilliseconds);

 

            Console.ReadLine();

        }

    }

The only difference between both Add operations is that in the first case, the typed Add method of the generically typed List reference is called. In the second case, the untyped Add method of the generically typed List is called through the IList reference.

On my slow Macbook, the first Add operation typically took between 10 and 20 ms. The second Add operation typically took almost twice as long as the first Add operation. As you can see, that is a very minor performance issue, and it actually is only consistently noticeable once you’re dealing with 100000 elements. At 50000 elements, both operations typically take the same amount of time with only minor variations in performance on certain runs.

So yes, once you’re dealing with a large enough set of elements, there is indeed a performance difference. But it’s extremely minor and the extra cost of the Add operation is most definitely the least of your concerns if you’re retrieving that many entity instances through an ORM. The extra amount of memory that needs to be used for those entities and the extra cost of pulling all of that data over the wire is what’s really going to bite you, not the extra cost of the Add operation ;)

Leave a Reply

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