Autofac(IOC框架)速度比拼
原帖:http://www.cnblogs.com/xupng/archive/2011/07/12/2104766.html
Autofac為何物?它是.NET世界裡現存的幾種IOC框架其中之一,傳說是速度最快的一個,同類的框架還有用過Castle Windsor、StructureMap、Unity等,如果你用過其中之一,那就好辦了*^_^*。什麼?你不知道什麼是IOC,好吧,這裡有一篇文章是講解IOC是何物的,IOC中文名被稱為依賴注入,看一下Martin Fowler大師寫的《IoC容器和 Dependency Injection 模式 》。
通過上面的瞭解,我們基本上已經明白了Autofac為何物了,現在瞭解一下它相對於其它的IoC框架有什麼優點:
- 它是C#語言聯絡很緊密,也就是說C#裡的很多程式設計方式都可以為Autofac使用,例如可以用Lambda表示式註冊元件。
- 較低的學習曲線,學習它非常的簡單,只要你理解了IoC和DI的概念以及在何時需要使用它們。
- XML配置支援。
- 自動裝配。
- 與Asp.Net MVC 3整合。
- 微軟的Orchad開源程式使用的就是Autofac,從該原始碼可以看出它的方便和強大。
效能上面呢,有人為其做了測試:
Announcements
This arctile is posted by Daniel Palme, who is a .NET consultant from Germany.I found it in his blog,and it's very helpful to me. So I decide to post it in my cnblogs. As it is so simple, as well as I've been used to read English articles. So I decided not
to translate it into Chinese to keep the original meaning. You can
In this post I will do a performance comparison of the most popular IoC containers.
Of course performance is not the only criteria when choosing a container for a project. Perhaps you need features like ,
then not all containers are suited. But if the container is "only" used for wiring up dependencies, why not choose the fastest one?
The test setup
The contestants
You probably have heard of most of the frameworks, but one of them is quite new and works differently: Hiro The author, Philip Laureano, names it an "inversion of control container compiler framework". After configuration, the container gets compiled and is immutable from now on. This concept makes this container really fast.
The test setup
To test the performance I register three interfaces and their corresponding implementation to every container. The first one is registered as a singleton, the second one as transient (a new instance is created every time). The third also has a transient lifetime, but moreover has a dependency to the first two interfaces (which get injected by constructor injection).
Every interface is resolved 1.000.000 times and the time is measured in milliseconds.
The results
Overview
Container | Singleton | Transient | Combined | Interception |
---|---|---|---|---|
No | 132 | 81 | 63 | |
AutoFac 3.0.2 | 749 | 1806 | 4570 | 33765 |
Caliburn.Micro 1.5.1 | 194 | 226 | 651 | |
Catel 3.5 | 258 | 952 | 2846 | |
Dynamo 3.0.1.0 | 72 | 84 | 139 | |
Funq 1.0.0.0 | 81 | 92 | 277 | |
Griffin 1.1.0 | 194 | 195 | 481 | |
Hiro 1.0.3 | 101 | 103 | 107 | |
LightCore 1.5.1 | 156 | 2343 | 12045 | |
LightInject 3.0.0.5 | 171 | 178 | 298 | |
LinFu 2.3.0.41559 | 2876 | 17564 | 47712 | |
Mef 4.0.0.0 | 2507 | 8724 | 24937 | |
MicroSliver 2.1.6.0 | 150 | 485 | 2019 | |
Mugen 3.5.1 | 302 | 483 | 1404 | |
Munq 3.1.6 | 102 | 131 | 412 | |
Ninject 3.0.1.10 | 6791 | 19314 | 54926 | |
Petite 0.3.2 | 86 | 86 | 260 | |
SimpleInjector 2.2.3 | 68 | 87 | 101 | 467 |
Speedioc 0.1.33 | 96 | 109 | 94 | |
Spring.NET 1.3.2 | 672 | 14560 | 39065 | 666 |
StructureMap 2.6.4.1 | 1127 | 1213 | 4379 | 7329 |
TinyIOC 1.2 | 246 | 1500 | 6098 | |
Unity 3.0.1304.0 | 2451 | 3807 | 11212 | 103682 |
Windsor 3.2.0 | 456 | 2422 | 6970 | 16677 |
Singleton
Transient
Combined
Feature comparison
Performance | Configuration | Features | Environment | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Container | Code | XML | Auto | Autowiring | Custom lifetimes | Interception | .NET | SL | WP7 | WinRT | |
AutoFac | Average | Yes | Yes | Yes | Yes | No | Yes | Yes | Yes | Yes | Yes |
Caliburn.Micro | Average | No | No | No | Yes | No | No | Yes | Yes | Yes | Yes |
Catel | Average | Yes | No | No | Yes | Yes | No | Yes | Yes | Yes | Yes |
Dynamo | Fast | Yes | No | Yes | Yes | Yes | No | Yes | No | No | No |
Funq | Fast | Yes | No | No | No | No | No | Yes | Yes | Yes | No |
Griffin | Fast | Yes | No | No | Yes | No | Yes | Yes | No | No | No |
Hiro | Fast | Yes | No | No | Yes | No | No | Yes | No | No | No |
LightCore | Average | Yes | Yes | Yes | Yes | Yes | No | Yes | Yes | No | No |
LightInject | Fast | Yes | No | Yes | No | Yes | No | Yes | Yes | No | No |
LinFu | Slow | Yes | No | No | Yes | No | Yes | Yes | No | No | No |
MEF | Slow | Yes | No | No | Yes | No | No | Yes | Yes | No | Yes |
MicroSliver | Average | Yes | No | No | Yes | No | No | Yes | Yes | No | Yes |
Mugen | Average | Yes | No | No | Yes | Yes | No | Yes | Yes | Yes | Yes |
Munq | Fast | Yes | No | No | Yes | Yes | No | Yes | No | Yes | No |
Ninject | Slow | Yes | No | Yes | Yes | Yes | No | Yes | Yes | Yes | Yes |
Petite | Fast | Yes | No | No | No | No | No | Yes | No | No | No |
SimpleInjector | Fast | Yes | No | Yes | Yes | Yes | Yes | Yes | Yes | No | No |
Speedioc | Fast | Yes | No | No | No | No | No | Yes | No | No | No |
Spring.NET | Very slow | No | Yes | No | Yes | No | Yes | Yes | No | No | No |
StructureMap | Average | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No | Yes | No |
TinyIoc | Average | Yes | No | Yes | Yes | Yes | No | Yes | Yes | Yes | No |
Unity | Average | Yes | Yes | No | Yes | Yes | Yes | Yes | Yes | No | Yes |
Windsor | Average | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No | No |
Conclusion
Ninject is definitely the slowest container.
MEF, LinFu and Spring.NET are faster than Ninject, but still pretty slow. AutoFac, Catel and Windsor come next, followed by StructureMap, Unity and LightCore. A disadvantage of Spring.NET is, that can only be configured with XML.
SimpleInjector, Hiro, Funq, Munq and Dynamo offer the best performance, they are extremely fast. Give them a try! Especially Simple Injector seems to be a good choice. It's very fast, has a good documentation and also supports advanced scenarios like interception and generic decorators.
Updates
13.09.2011: Funq and Munq have been added to the list of contestants, both frameworks are really fast. The updated charts do no more contain Spring.NET, since it was extremly slow.
04.11.2011: I added Simple Injector, the performance is the best of all contestants.
16.12.2011: I added Dynamo.Ioc, the performance is very close to Simple Injector and Hiro.
22.01.2012: Added TinyIoc.
22.02.2012: Updated IServiceLocator implementations.
12.03.2012: Added LightInject. Added feature comparison.
25.04.2012: Updated to Ninject 3.0.015 and Petite 0.3.2.
14.05.2012: Added Mugen.
14.06.2012: Added MEF.
18.06.2012: Added Griffin.
20.08.2012: Updated to Castle Windsor 3.1.0, LightInject 2.0.0.0, Simple Injector 1.5.0.12199, Structuremap 2.6.4.1, MugenInjection 2.6.0 and Unity 2.1.505.2
18.09.2012: Added Catel.
15.10.2012: Updated to Dynamo.Ioc 3.0.1.0 and MugenInjection 3.0.0
15.12.2012: Updated to Catel 3.4, Griffin.Container 1.1.0, SimpleInjector 1.6.0.12319, TinyIoC 1.2
01.01.2013: Added Caliburn.Micro 1.4
06.01.2013: Added Speedioc. Updated to Autofac 3.0.0, Caliburn.Micro.Container 1.4.1, LightCore 1.5.0
26.02.2013: Updated to Autofac 3.0.1, LightCore 1.5.1, Windsor 3.2.0
15.03.2013: Added benchmark for interception
03.04.2013: Added MicroSliver
11.04.2013: Updated several containers
09.05.2013: Updated LightInject, SimpleInjector and Unity
Source code
Latest source code is available on