-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
152 lines (138 loc) · 3.84 KB
/
Copy pathProgram.cs
File metadata and controls
152 lines (138 loc) · 3.84 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
namespace ReflectionBenchmark
{
class Program
{
private class Timer : IDisposable
{
private readonly Stopwatch watch;
public Timer()
{
watch = Stopwatch.StartNew();
}
public void Dispose()
{
watch.Stop();
Console.WriteLine("{0}ms", watch.ElapsedMilliseconds);
}
}
private class Scope : IDisposable
{
public void Dispose()
{
}
}
const int LoopLimit = 100000000;
delegate void SetterAction<in T, in TArg>(T self, TArg value);
static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine("no-reflection setter");
Console.WriteLine("Runtime: ");
using (new Scope())
{
var example = new ExampleType();
using (new Timer())
for (int i = 0; i < LoopLimit; i++)
{
example.Property = "hello world";
}
}
Console.WriteLine();
Console.WriteLine("setter via direct reflection");
using (new Scope())
{
var example = new ExampleType();
MethodInfo setter;
Console.Write("Setup: ");
using (new Timer())
{
setter = typeof(ExampleType).GetProperty("Property").GetSetMethod();
}
Console.Write("Runtime: ");
using (new Timer())
for (int i = 0; i < LoopLimit; i++)
{
setter.Invoke(example, new[]{"hello world"});
}
}
Console.WriteLine();
Console.WriteLine("setter via dynamic dispatch");
using (new Scope())
{
dynamic example = new ExampleType();
Console.Write("Runtime: ");
using (new Timer())
for (int i = 0; i < LoopLimit; i++)
{
example.Property = "hello world";
}
}
Console.WriteLine();
Console.WriteLine("setter via Action<ExampleType, string>");
using (new Scope())
{
Console.Write("Setup: ");
var example = new ExampleType();
Action<ExampleType, string> setter;
using (new Timer())
{
setter = (x, y) => x.Property = y;
}
Console.Write("Runtime: ");
using (new Timer())
for (int i = 0; i < LoopLimit; i++)
{
setter(example, "hello world");
}
}
Console.WriteLine();
Console.WriteLine("setter via direct expression tree");
using (new Scope())
{
var example = new ExampleType();
Action<ExampleType, string> action;
Console.Write("Setup: ");
using (new Timer())
{
var setter = typeof(ExampleType).GetProperty("Property").GetSetMethod();
var self = Expression.Parameter(typeof(ExampleType));
var value = Expression.Parameter(typeof(string));
var assignment = Expression.Assign(Expression.Property(self, typeof(ExampleType).GetProperty("Property")), value);
var lambda = Expression.Lambda<Action<ExampleType, string>>(assignment, self, value);
action = lambda.Compile();
}
Console.Write("Runtime: ");
using (new Timer())
for (int i = 0; i < LoopLimit; i++)
{
action(example, "hello world");
}
}
Console.WriteLine();
Console.WriteLine("setter via indirect expression tree");
using (new Scope())
{
var example = new ExampleType();
var setter = typeof(ExampleType).GetProperty("Property").GetSetMethod();
var self = Expression.Parameter(typeof(ExampleType));
var value = Expression.Parameter(typeof(string));
var assignment = Expression.Assign(Expression.Property(self, typeof(ExampleType).GetProperty("Property")), value);
var lambda = Expression.Lambda(assignment, self, value);
dynamic action = lambda.Compile();
Console.Write("Runtime: ");
using (new Timer())
for (int i = 0; i < LoopLimit; i++)
{
action.Invoke(example, "hello world");
}
}
}
}
}