Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.






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.

4 DI/IoC containers: creating 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.

4 DI/IoC containers: creating complex objects

Here are comparison chart of results for both measurements.

4 DI/IoC containers: performance comparison chart

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.


Damien said:

Should have included autofac

# September 21, 2010 8:08 PM

Robert said:

Where's Autofac?

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

# September 22, 2010 2:03 AM

DigiMortal said:

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 5:38 AM

AlecWhittington said:

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 2:42 PM

Cecil said:

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

# September 22, 2010 4:24 PM

Chris Martin said:

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

# September 23, 2010 12:27 AM

j.channon said:

Please state which versions you use

# January 19, 2011 11:28 AM

John said:

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?

# October 4, 2011 6:08 AM

Tom said:

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

# January 13, 2012 10:40 AM

rsuharta said:

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

# February 15, 2012 8:14 PM

renso said:

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 8:41 AM

DigiMortal said:

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

# July 2, 2012 12:00 PM