Unity, Castle Windsor, StructureMap, Ninject – who has best performance?

I made quick comparison of performance of four DI/IoC containers. I measured Unity,Castle Windsor, StructureMap and Ninject in two scenarios – resolving objects with empty constructor and resolving object with injected parameters in its constructor. Results are here.

As a first thing I made carefully sure that objects are created when resolve methods are called. The results for resolving object with default constructor by interface are here.

Initializing objects with default constructor

In second round I let mappers resolve type that needs constructor parameters and these parameters were injected my DI/IoC containers. Times changed a little bit but TOP4 stayed the same.

Objects with parameterized constructors

Here are comparison chart of results for both measurements.

Resolving comparison

StructureMap that is old veteran on field when considering .NET gives best results. Unity and Castle Windsor have also good results although Unity works faster in more complex scenarios. Ninject was slowest, although they use lightweight code generation (LCG). You can also read about my object to object mapper experiment and LCG to get better idea what LCG is all about.

Currently, I think, I will stick with StructureMap as they also have pretty good fluent API that was asy to learn and easy to use. I also hope that other competitors give better results in their next versions.

Update 1: Autofac gave me the following results – 9 and 16 for first and second test. So it seems like they are fastest ones. Thanks for suggestion, Robert!

Update 2: Units on charts are ticks. It is average over one million calls to pre-warmed resolve.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    12 thoughts on “Unity, Castle Windsor, StructureMap, Ninject – who has best performance?

    • September 22, 2010 at 12:08 am
      Permalink

      Should have included autofac

    • September 22, 2010 at 6:03 am
      Permalink

      Where’s Autofac?

      And what’s the scale? Is a higher bar better, or worse?

    • September 22, 2010 at 9:38 am
      Permalink

      Thanks for feedback Damien and Robert! I added Autofac results too and seems like Autofac gives best performance right now. I also made update about units used and it is easy to understand now that small results are better.

    • September 22, 2010 at 6:42 pm
      Permalink

      What version of each did you use? I know castle 2.1 had major performance improvements over the previous version and the latest stable version is 2.5

    • September 22, 2010 at 8:24 pm
      Permalink

      I think you should state what versions of each of these containers were used for the comparison..

    • September 23, 2010 at 4:27 am
      Permalink

      Not that I doubt the results, but I think it would also be fair to attach the “runnable” tests.

    • January 19, 2011 at 4:28 pm
      Permalink

      Please state which versions you use

    • October 4, 2011 at 10:08 am
      Permalink

      When you publish a diagram you should ALWAYS write the unit of measurements.
      From the first diagram:
      Ninject = 80 while Unity = 20

      80 what? 20 what?

    • January 13, 2012 at 3:40 pm
      Permalink

      What’s the scale? Nanoseconds? Microseconds? Clock ticks?

    • February 16, 2012 at 1:14 am
      Permalink

      yes.. what is the scale? how did you measure the performance (in what way)?? this article is quite vague…

    • July 2, 2012 at 12:41 pm
      Permalink

      This is very useful, although not conclusive in making a decisive decision on which one to use, rather which one not to use based on poor performance. Have used Castle Windsor for a log time, but it so simple I don’t think it’s a big deal switching to another as long as they can support what I need.

    • July 2, 2012 at 4:00 pm
      Permalink

      renso, as long as you don’t have problems with performance change nothing. Before optimizing something for performance you must have performance problem. Otherwise there is nothing to optimize :)

    Leave a Reply

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