X

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.

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.

Here are comparison chart of results for both measurements.

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.

Liked this post? Empower your friends by sharing it!
Categories: C# Tools

View Comments (12)

  • 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.

  • 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

  • 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?

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

  • 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.

  • 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 :)

Related Post