DI Performance Tests

Oct 27, 2009 at 4:54 PM
Edited Oct 27, 2009 at 6:22 PM

Curious if anyone has any performance information when using Unity or any other DI framework in a large application with many container controlled objects? 

I have created a small test to instantiate one object 4 different ways.
My test object does not have any DI injected types.
When creating 100000 objects here are the results in milliseconds: 

Create by Activator.CreateInstance<>
206 milliseconds 

Create by using a IL generated Dynamically Created Method and Delegate and storing the delegate in cache for subsequent instantiations:
49 milliseconds 

Create by standard new MyObeject()
10 milliseconds 

Create by Unity Resolve (Note: container was created, configured and stored in a local var one time, before starting the Stopwatched)
475 milliseconds 

 Create by Ninject Resolve
1850 milliseconds 

I tested two DI frameworks, Unity and Ninject. Unity is able to resolve my type much faster then Ninject, but is still 2x slower then  Activator.CreateInstance.
Does anyone have any thoughts on the overall performance impact application wide when using such a framework. 

I really believe that a substantial performance hit is warranted giving the architecture gains DI provides, but I am realistically trying evaluate the performance impact. 

Thanks.
Joe