-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmarks.cs
66 lines (56 loc) · 1.62 KB
/
Benchmarks.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
using BenchmarkDotNet.Attributes;
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace InstanciateBenchmarks
{
[ClrJob(baseline: true), CoreJob, CoreRtJob]
[RPlotExporter, RankColumn]
public class Benchmarks
{
private Type _type;
private ConstructorInfo _constructor;
private Delegate _delegate;
private Func<object> _func;
private Func<TestClass> _typedFunc;
[GlobalSetup]
public void GlobalSetup()
{
_type = typeof(TestClass);
_constructor = _type.GetConstructor(Type.EmptyTypes);
_delegate = Expression.Lambda(Expression.New(_type)).Compile();
_func = Expression.Lambda<Func<object>>(Expression.New(_type)).Compile();
_typedFunc = Expression.Lambda<Func<TestClass>>(Expression.New(_type)).Compile();
}
[Benchmark(Baseline = true)]
public TestClass New()
{
return new TestClass();
}
[Benchmark]
public TestClass Activator()
{
return (TestClass)System.Activator.CreateInstance(_type);
}
[Benchmark]
public TestClass Constructor()
{
return (TestClass)_constructor.Invoke(null);
}
[Benchmark]
public TestClass Delegate()
{
return (TestClass)_delegate.DynamicInvoke();
}
[Benchmark]
public TestClass Func()
{
return (TestClass)_func();
}
[Benchmark]
public TestClass TypedFunc()
{
return _typedFunc();
}
}
}