IoC performance – StructureMap

This is a continuation of NInject performance, in a way.

Ran same test with StructureMap, and results look much better comparing to Ninject: 309ms to create 100k objects using StructureMap vs 2ms to create same number of objects just using constructors. That’s about 7x faster than Ninject.

Again, here’s the code I used to test (though it didn’t change much).

 

class StructureMapPerfTest
    {
        public StructureMapPerfTest()
        {
            this.InitIoC();
        }
 
        private void InitIoC()
        {
            ObjectFactory.Initialize(x =>
            {
                x.For<IWorker>().Use<Worker>();
            });
        }
 
        public void Test()
        {
            int count = 100000;
 
            DateTime start = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                IWorker w = ObjectFactory.GetInstance<IWorker>();
            }
            Debug.WriteLine(DateTime.Now.Subtract(start).TotalMilliseconds + " ms with Ioc");
 
            start = DateTime.Now;
            for (int i = 0; i < count; i++)
            {
                IWorker w = new Worker();
            }
            Debug.WriteLine(DateTime.Now.Subtract(start).TotalMilliseconds + " ms with constructor");
        }
 
        interface IWorker
        {
            void DoWork();
        }
 
        class Worker : IWorker
        {
            public void DoWork()
            {
                Console.WriteLine("Working hard");
            }
        }
     }

Leave a Reply

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