转一篇关于ioc性能测试的文章

Posted: October 15, 2017
By: Adam Pedley

Types

1
2
3
4
5
6
7
public interface ISecond { }
public class Second : ISecond { }
public interface IExample { }
public class Example : IExample
{
public Example(ISecond second) { }
}

Test 1

1
2
3
4
5
Stopwatch s = new Stopwatch();
s.Start();
Locator.CurrentMutable.RegisterLazySingleton(() => new Second(), typeof(ISecond));
Locator.CurrentMutable.RegisterLazySingleton(() => new Example(Locator.Current.GetService<ISecond>()), typeof(IExample));
s.Stop();

Test 2

1
2
3
4
Stopwatch s = new Stopwatch();
s.Start();
Locator.Current.GetService<ISecond>();
s.Stop();

Test 3

1
2
3
4
Stopwatch s = new Stopwatch();
s.Start();
Locator.Current.GetService<IExample>();
s.Stop();

Results

Framework Register Type Resolve Resolve with CI
AutoFac (4.6.1) 212ms 284ms 332ms
Caliburn.Micro (3.2.0) 20ms 134ms 140ms
DryIoc (2.12.3) 138ms 276ms 294ms
FreshMVVM (2.2.0) 18ms 128ms 131ms
MVVMCross (5.3.0) 5ms 118ms 123ms
MVVMLight (5.3.0) 16ms 118ms 128ms
Splat (2.0.0) 22ms 105ms 111ms
Unity (5.0-rc1) 76ms 515ms 593ms
Xamarin.Forms (2.4.0) 1ms 99ms N/A