Autofac(IOC框架)速度比拼

来源:互联网 发布:实验数据记录怎么写 编辑:程序博客网 时间:2024/04/30 20:49

原帖: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-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

ContainerSingletonTransientCombinedInterceptionNo1328163 AutoFac 3.0.27491806457033765Caliburn.Micro 1.5.1194226651 Catel 3.52589522846 Dynamo 3.0.1.07284139 Funq 1.0.0.08192277 Griffin 1.1.0194195481 Hiro 1.0.3101103107 LightCore 1.5.1156234312045 LightInject 3.0.0.5171178298 LinFu 2.3.0.4155928761756447712 Mef 4.0.0.02507872424937 MicroSliver 2.1.6.01504852019 Mugen 3.5.13024831404 Munq 3.1.6102131412 Ninject 3.0.1.1067911931454926 Petite 0.3.28686260 SimpleInjector 2.2.36887101467Speedioc 0.1.339610994 Spring.NET 1.3.26721456039065666StructureMap 2.6.4.11127121343797329TinyIOC 1.224615006098 Unity 3.0.1304.02451380711212103682Windsor 3.2.04562422697016677

 Singleton

Transient

Combined

Feature comparison

 PerformanceConfigurationFeaturesEnvironmentContainer CodeXMLAutoAutowiringCustom lifetimesInterception.NETSLWP7WinRTAutoFacAverageYesYesYesYesNoYesYesYesYesYesCaliburn.MicroAverageNoNoNoYesNoNoYesYesYesYesCatelAverageYesNoNoYesYesNoYesYesYesYesDynamoFastYesNoYesYesYesNoYesNoNoNoFunqFastYesNoNoNoNoNoYesYesYesNoGriffinFastYesNoNoYesNoYesYesNoNoNoHiroFastYesNoNoYesNoNoYesNoNoNoLightCoreAverageYesYesYesYesYesNoYesYesNoNoLightInjectFastYesNoYesNoYesNoYesYesNoNoLinFuSlowYesNoNoYesNoYesYesNoNoNoMEFSlowYesNoNoYesNoNoYesYesNoYesMicroSliverAverageYesNoNoYesNoNoYesYesNoYesMugenAverageYesNoNoYesYesNoYesYesYesYesMunqFastYesNoNoYesYesNoYesNoYesNoNinjectSlowYesNoYesYesYesNoYesYesYesYesPetiteFastYesNoNoNoNoNoYesNoNoNoSimpleInjectorFastYesNoYesYesYesYesYesYesNoNoSpeediocFastYesNoNoNoNoNoYesNoNoNoSpring.NETVery slowNoYesNoYesNoYesYesNoNoNoStructureMapAverageYesYesYesYesYesYesYesNoYesNoTinyIocAverageYesNoYesYesYesNoYesYesYesNoUnityAverageYesYesNoYesYesYesYesYesNoYesWindsorAverageYesYesYesYesYesYesYesYesNoNo

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.


0 0