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框架有什么优点:

  1. 它是C#语言联系很紧密,也就是说C#里的很多编程方式都可以为Autofac使用,例如可以用Lambda表达式注册组件。
  2. 较低的学习曲线,学习它非常的简单,只要你理解了IoC和DI的概念以及在何时需要使用它们。
  3. XML配置支持。
  4. 自动装配。
  5. 与Asp.Net MVC 3集成。
  6. 微软的Orchad开源程序使用的就是Autofac,从该源码可以看出它的方便和强大。

性能上面呢,有人为其做了测试:

Autofac(IOC框架)速度比拼_第1张图片

Autofac(IOC框架)速度比拼_第2张图片

说了这么多,在哪里可以得到它呢?你可以直接点击这里下载Autofac-Net4.0 ,也可以去它的官方网站上http://code.google.com/p/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 click this link to access the original.

 

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 interception , 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

  • AutoFac
  • Caliburn.Micro
  • Catel
  • Dynamo.Ioc
  • Funq
  • Griffin
  • Hiro
  • LightCore
  • LightInject
  • LinFu
  • MEF
  • MicroSliver
  • Mugen
  • Munq
  • Ninject
  • Petite
  • Simple Injector
  • Speedioc
  • Spring.NET
  • StructureMap
  • TinyIoc
  • Unity
  • Windsor

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

Autofac(IOC框架)速度比拼_第3张图片

Transient

Autofac(IOC框架)速度比拼_第4张图片

Combined

Autofac(IOC框架)速度比拼_第5张图片

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


你可能感兴趣的:(C#/VS.Net/Mono)