UnityContainer Performance Test Results

As part of the basic understanding of unity, I created the following console application (long long):

IUnityContainer container = new UnityContainer();
        container.RegisterType<ISimpleClass, SimpleClass>();
        container.RegisterType<ISingletonWithUnity, SingletonWithUnity>(new ContainerControlledLifetimeManager());

        double N = 10000;
        double sum = 0;

        Console.WriteLine("Testing performace of a basic new object");
        for (int i = 0; i < N; i++)
        {
            DateTime start = DateTime.Now;
            ISimpleClass simpleClass = new SimpleClass();
            DateTime end = DateTime.Now;
            sum += (end - start).Milliseconds;
        }
        double average = sum/N;
        Console.WriteLine("Average time for basic new object is: " + average);


        Console.WriteLine("Testing performance of transient resolve using unity");
        sum = 0;
        for (int i = 0; i < N; i++)
        {
            DateTime start = DateTime.Now;
            ISimpleClass simpleClass = container.Resolve<SimpleClass>();
            DateTime end = DateTime.Now;
            sum += (end - start).Milliseconds;
        }
        average = sum / N;

        Console.WriteLine("Average time for transient resolve using unity is: " + average);


        Console.WriteLine("Testing performance of basic singleton");
        sum = 0;
        for (int i = 0; i < N; i++)
        {
            DateTime start = DateTime.Now;
            BasicSingltonClass basicSingltonClass = BasicSingltonClass.Instance;
            DateTime end = DateTime.Now;
            sum += (end - start).Milliseconds;
        }
        average = sum / N;

        Console.WriteLine("Average time for basic singleton is: " + average);

        Console.WriteLine("Testing performance of unity singleton");
        sum = 0;
        for (int i = 0; i < N; i++)
        {
            DateTime start = DateTime.Now;
            SingletonWithUnity singletonWithUnity = container.Resolve<SingletonWithUnity>();
            DateTime end = DateTime.Now;
            sum += (end - start).Milliseconds;
        }
        average = sum / N;

        Console.WriteLine("Average time for unity singleton is: " + average);

The classes used in the test are pretty straightforward:

  • SimpleClass is just an empty class
  • BasicSingletonClass is exactly what regular Double Checked Locking uses on getter instance.
  • SingletonWithUnity is also just an empty class, but as you can see, it is registered with ContainerControlleredLifetimeManager.

The results that I get as N goes higher (up to 1 million) show that using unity is actually much slower than using it. I used to think that Unity has some kind of permission caching and does it much faster, especially in transitional resolution.

, N 10 000:

: 0,0007

: 0,008

: 0,0012

Singleton Singleton: 0,0033

, .,.

, Unity - . , , .

!

+3
2

, Unity , , , . , (, ) .

. DI, .

+7

, , . , . , , !

0

All Articles