-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConstructorPerformance.cs
92 lines (77 loc) · 2.75 KB
/
ConstructorPerformance.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
using BenchmarkDotNet.Attributes;
using FastExpressionCompiler;
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace ExpressionDelegates.Benchmarks
{
public class ConstructorPerformance
{
public Expression<Func<TestClass>> Expression = () => new TestClass();
private readonly ConstructorInfo _constructorInfo;
private readonly Func<TestClass> _directDelegate = () => new TestClass();
private readonly Func<TestClass> _cachedCompile;
private readonly Func<TestClass> _cachedCompileFast;
private readonly Func<TestClass> _cachedInterpret;
private readonly Constructor _cachedConstructor;
public ConstructorPerformance()
{
_constructorInfo = ((NewExpression)Expression.Body).Constructor;
_cachedCompile = Expression.Compile();
_cachedCompileFast = Expression.CompileFast();
_cachedInterpret = Expression.Compile(preferInterpretation: true);
_cachedConstructor = Constructors.Find(_constructorInfo);
}
[Benchmark(Description = "Direct Delegate Invoke")]
public void DirectDelegate()
{
_directDelegate.Invoke();
}
[Benchmark(Description = "Cached Compile Invoke")]
public void CompileCache()
{
_cachedCompile.Invoke();
}
[Benchmark(Description = "Cached CompileFast Invoke")]
public void CompileFastCache()
{
_cachedCompileFast.Invoke();
}
[Benchmark(Description = "Cached ExpressionDelegates.Constructor Invoke")]
public void ExpressionDelegatesConstructorCache()
{
_cachedConstructor.Invoke();
}
[Benchmark(Description = "ConstructorInfo.Invoke")]
public void Reflection()
{
_constructorInfo.Invoke(null);
}
[Benchmark(Description = "ExpressionDelegates.Constructor Find and Invoke")]
public void ExpressionDelegatesConstructor()
{
var ctor = Constructors.Find(_constructorInfo);
ctor.Invoke();
}
[Benchmark(Description = "Cached Interpretation Invoke")]
public void InterpretCache()
{
_cachedInterpret.Invoke();
}
[Benchmark(Description = "Interpret and Invoke")]
public void Interpret()
{
Expression.Compile(preferInterpretation: true).Invoke();
}
[Benchmark(Description = "Compile and Invoke")]
public void Compile()
{
Expression.Compile().Invoke();
}
[Benchmark(Description = "CompileFast and Invoke")]
public void CompileFast()
{
Expression.CompileFast().Invoke();
}
}
}