-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetterPerformance.cs
110 lines (93 loc) · 3.56 KB
/
GetterPerformance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using BenchmarkDotNet.Attributes;
using FastExpressionCompiler;
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace ExpressionDelegates.Benchmarks
{
public class GetterPerformance
{
public Expression<Func<TestClass, int>> Expression = s => s.Property;
private readonly TestClass _obj = new TestClass();
private readonly PropertyInfo _propertyInfo;
private readonly Func<TestClass, int> _directDelegate = s => s.Property;
private readonly Func<TestClass, int> _cachedCompile;
private readonly Func<TestClass, int> _cachedCompileFast;
private readonly Func<TestClass, int> _cachedInterpret;
private readonly Func<TestClass, int> _cachedCreateDelegate;
private readonly Accessor _cachedAccessor;
public GetterPerformance()
{
_propertyInfo = (PropertyInfo)((MemberExpression)Expression.Body).Member;
_cachedCompile = Expression.Compile();
_cachedCompileFast = Expression.CompileFast();
_cachedInterpret = Expression.Compile(preferInterpretation: true);
_cachedCreateDelegate = (Func<TestClass, int>)_propertyInfo.GetMethod
.CreateDelegate(typeof(Func<TestClass, int>));
_cachedAccessor = Accessors.Find(_propertyInfo);
}
[Benchmark(Description = "Cached Compile Invoke")]
public void CompileCache()
{
_cachedCompile.Invoke(_obj);
}
[Benchmark(Description = "Cached CompileFast Invoke")]
public void CompileFastCache()
{
_cachedCompileFast.Invoke(_obj);
}
[Benchmark(Description = "Direct Delegate Invoke")]
public void DirectDelegate()
{
_directDelegate.Invoke(_obj);
}
[Benchmark(Description = "Cached CreateDelegate Invoke")]
public void CreateDelegateCache()
{
_cachedCreateDelegate.Invoke(_obj);
}
[Benchmark(Description = "Cached ExpressionDelegates.Accessor Invoke")]
public void ExpressionDelegatesAccessorCache()
{
_cachedAccessor.Get(_obj);
}
[Benchmark(Description = "Cached Interpretation Invoke")]
public void InterpretCache()
{
_cachedInterpret.Invoke(_obj);
}
[Benchmark(Description = "PropertyInfo.GetValue")]
public void Reflection()
{
var propertyInfo = (PropertyInfo)((MemberExpression)Expression.Body).Member;
propertyInfo.GetValue(_obj);
}
[Benchmark(Description = "ExpressionDelegates.Accessor Find and Invoke")]
public void ExpressionDelegatesAccessor()
{
var accessor = Accessors.Find(_propertyInfo);
accessor.Get(_obj);
}
[Benchmark(Description = "CreateDelegate and Invoke")]
public void CreateDelegate()
{
var delegatee = (Func<TestClass, int>)_propertyInfo.GetMethod.CreateDelegate(typeof(Func<TestClass, int>));
delegatee.Invoke(_obj);
}
[Benchmark(Description = "Interpret and Invoke")]
public void Interpret()
{
Expression.Compile(preferInterpretation: true).Invoke(_obj);
}
[Benchmark(Description = "Compile and Invoke")]
public void Compile()
{
Expression.Compile().Invoke(_obj);
}
[Benchmark(Description = "CompileFast and Invoke")]
public void CompileFast()
{
Expression.CompileFast().Invoke(_obj);
}
}
}