-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDynamicVsGenericBenchmarks.cs
More file actions
211 lines (183 loc) · 6.75 KB
/
Copy pathDynamicVsGenericBenchmarks.cs
File metadata and controls
211 lines (183 loc) · 6.75 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Numerics;
using BenchmarkDotNet.Attributes;
#pragma warning disable CA1822 // Mark members as static
namespace Platform.Data.Doublets.Benchmarks
{
/// <summary>
/// Performance comparison between different approaches for generic operations.
/// Based on https://stackoverflow.com/a/8122675/710069
/// </summary>
[SimpleJob]
[MemoryDiagnoser]
public class DynamicVsGenericBenchmarks
{
private const int Iterations = 1000;
private readonly ulong[] _values;
public DynamicVsGenericBenchmarks()
{
_values = new ulong[Iterations];
var random = new System.Random(42);
for (int i = 0; i < Iterations; i++)
{
_values[i] = (ulong)random.Next(1, 100);
}
}
[GlobalSetup]
public void Setup()
{
// Warm up expression compilation
ExpressionAdd(1UL, 2UL);
ExpressionEquals(1UL, 2UL);
}
[Benchmark(Description = "INumber Addition")]
public ulong INumberAddition()
{
ulong sum = 0;
for (int i = 0; i < Iterations - 1; i++)
{
sum += INumberAdd(_values[i], _values[i + 1]);
}
return sum;
}
[Benchmark(Description = "Dynamic Addition")]
public ulong DynamicAddition()
{
ulong sum = 0;
for (int i = 0; i < Iterations - 1; i++)
{
sum += DynamicAdd(_values[i], _values[i + 1]);
}
return sum;
}
[Benchmark(Description = "Expression Addition")]
public ulong ExpressionAddition()
{
ulong sum = 0;
for (int i = 0; i < Iterations - 1; i++)
{
sum += ExpressionAdd(_values[i], _values[i + 1]);
}
return sum;
}
[Benchmark(Description = "IEqualityOperators Equality")]
public int IEqualityOperatorsEquality()
{
int equalCount = 0;
for (int i = 0; i < Iterations - 1; i++)
{
if (IEqualityOperatorsEquals(_values[i], _values[i + 1]))
equalCount++;
}
return equalCount;
}
[Benchmark(Description = "EqualityComparer Equality")]
public int EqualityComparerEquality()
{
int equalCount = 0;
for (int i = 0; i < Iterations - 1; i++)
{
if (EqualityComparerEquals(_values[i], _values[i + 1]))
equalCount++;
}
return equalCount;
}
[Benchmark(Description = "Dynamic Equality")]
public int DynamicEquality()
{
int equalCount = 0;
for (int i = 0; i < Iterations - 1; i++)
{
if (DynamicEquals(_values[i], _values[i + 1]))
equalCount++;
}
return equalCount;
}
[Benchmark(Description = "Expression Equality")]
public int ExpressionEquality()
{
int equalCount = 0;
for (int i = 0; i < Iterations - 1; i++)
{
if (ExpressionEquals(_values[i], _values[i + 1]))
equalCount++;
}
return equalCount;
}
// Implementation methods
// INumber approach (NET 7+ - best performance)
private static T INumberAdd<T>(T a, T b) where T : INumber<T>
{
return a + b;
}
// Dynamic approach (slowest)
private static T DynamicAdd<T>(T a, T b)
{
dynamic dynamicA = a;
dynamic dynamicB = b;
dynamic result = dynamicA + dynamicB;
return (T)Convert.ChangeType(result, typeof(T));
}
// Expression-based approach (moderate performance)
private static readonly ConcurrentDictionary<Type, object> _addFunctions =
new ConcurrentDictionary<Type, object>();
private static T ExpressionAdd<T>(T a, T b)
{
var addFunc = (Func<T, T, T>)_addFunctions.GetOrAdd(typeof(T), type =>
{
var paramA = Expression.Parameter(type, "a");
var paramB = Expression.Parameter(type, "b");
Expression left = paramA;
Expression right = paramB;
// For byte and other smaller types, convert to int for arithmetic
if (type == typeof(byte) || type == typeof(sbyte) || type == typeof(short) || type == typeof(ushort))
{
left = Expression.Convert(paramA, typeof(int));
right = Expression.Convert(paramB, typeof(int));
}
Expression body = Expression.Add(left, right);
// Convert back if needed
if (type == typeof(byte) || type == typeof(sbyte) || type == typeof(short) || type == typeof(ushort))
{
body = Expression.Convert(body, type);
}
return Expression.Lambda<Func<T, T, T>>(body, paramA, paramB).Compile();
});
return addFunc(a, b);
}
// IEqualityOperators approach (NET 7+ - best performance)
private static bool IEqualityOperatorsEquals<T>(T a, T b) where T : IEqualityOperators<T, T, bool>
{
return a == b;
}
// EqualityComparer approach (good performance)
private static bool EqualityComparerEquals<T>(T a, T b)
{
return EqualityComparer<T>.Default.Equals(a, b);
}
// Dynamic approach (slowest)
private static bool DynamicEquals<T>(T a, T b)
{
dynamic dynamicA = a;
dynamic dynamicB = b;
return (bool)(dynamicA == dynamicB);
}
// Expression-based approach (moderate performance)
private static readonly ConcurrentDictionary<Type, object> _equalityFunctions =
new ConcurrentDictionary<Type, object>();
private static bool ExpressionEquals<T>(T a, T b)
{
var equalityFunc = (Func<T, T, bool>)_equalityFunctions.GetOrAdd(typeof(T), type =>
{
var paramA = Expression.Parameter(type, "a");
var paramB = Expression.Parameter(type, "b");
var body = Expression.Equal(paramA, paramB);
return Expression.Lambda<Func<T, T, bool>>(body, paramA, paramB).Compile();
});
return equalityFunc(a, b);
}
}
}