Skip to content

Commit 42c3fa8

Browse files
committed
[Benchmark] add benchmark on using.
1 parent c244ddd commit 42c3fa8

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.CompilerServices;
5+
using BenchmarkDotNet.Attributes;
6+
using StructLinq.Array;
7+
8+
namespace StructLinq.Benchmark
9+
{
10+
//BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
11+
//Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
12+
//.NET Core SDK=3.1.101
13+
//[Host] : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
14+
//DefaultJob : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
15+
16+
17+
//```
18+
//| Method | Mean | Error | StdDev | Median | Ratio | RatioSD |
19+
//|------------------------------------ |----------:|----------:|----------:|----------:|------:|--------:|
20+
//| ForEachWithUsingOnStruct | 5.976 us | 0.1193 us | 0.2645 us | 5.880 us | 1.00 | 0.00 |
21+
//| ForEachWithoutUsingOnStruct | 2.950 us | 0.0562 us | 0.0711 us | 2.921 us | 0.49 | 0.02 |
22+
//| ForEachWithTryFinallyOnStruct | 5.858 us | 0.1554 us | 0.1662 us | 5.796 us | 0.98 | 0.06 |
23+
//| ForEachWithUsingOnClass | 31.910 us | 0.6356 us | 0.6527 us | 31.543 us | 5.36 | 0.26 |
24+
//| ForEachWithoutUsingOnClass | 15.145 us | 0.1289 us | 0.1077 us | 15.123 us | 2.56 | 0.09 |
25+
//| ForEachWithTryFinallyOnClass | 32.244 us | 0.6352 us | 0.9889 us | 32.025 us | 5.38 | 0.35 |
26+
27+
28+
[DisassemblyDiagnoser(recursiveDepth: 4)]
29+
public class ImpactOfUsingOnForEach
30+
{
31+
private const int Count = 10000;
32+
33+
private ArrayEnumerable<Class> arrayOfClass;
34+
private ArrayEnumerable<Struct> arrayOfStruct;
35+
36+
public ImpactOfUsingOnForEach()
37+
{
38+
arrayOfClass = Enumerable.Range(0, Count).Select(x => new Class(x)).ToArray().ToTypedEnumerable();
39+
arrayOfStruct = Enumerable.Range(0, Count).Select(x => new Struct(x)).ToArray().ToTypedEnumerable();
40+
}
41+
42+
[Benchmark(Baseline = true)]
43+
public void ForEachWithUsingOnStruct()
44+
{
45+
ForEachWithUsing(ref arrayOfStruct, x => x);
46+
}
47+
48+
[Benchmark]
49+
public void ForEachWithoutUsingOnStruct()
50+
{
51+
ForEachWithoutUsing(ref arrayOfStruct, x => x);
52+
}
53+
54+
[Benchmark]
55+
public void ForEachWithTryFinallyOnStruct()
56+
{
57+
ForEachWithTryFinally(ref arrayOfStruct, x => x);
58+
}
59+
60+
[Benchmark]
61+
public void ForEachWithUsingOnClass()
62+
{
63+
ForEachWithUsing(ref arrayOfClass, x => x);
64+
}
65+
66+
67+
[Benchmark]
68+
public void ForEachWithoutUsingOnClass()
69+
{
70+
ForEachWithoutUsing(ref arrayOfClass, x => x);
71+
}
72+
73+
[Benchmark]
74+
public void ForEachWithTryFinallyOnClass()
75+
{
76+
ForEachWithTryFinally(ref arrayOfClass, x => x);
77+
}
78+
79+
80+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
81+
public static void ForEachWithoutUsing<T, TEnumerator, TEnumerable>(ref TEnumerable enumerable,
82+
Func<TEnumerable, IStructEnumerable<T, TEnumerator>> _)
83+
where TEnumerator : struct, IEnumerator<T>
84+
where TEnumerable : struct, IStructEnumerable<T, TEnumerator>
85+
{
86+
var enumerator = enumerable.GetStructEnumerator();
87+
while (enumerator.MoveNext())
88+
{
89+
}
90+
91+
}
92+
93+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
94+
public static void ForEachWithUsing<T, TEnumerator, TEnumerable>(ref TEnumerable enumerable,
95+
Func<TEnumerable, IStructEnumerable<T, TEnumerator>> _)
96+
where TEnumerator : struct, IEnumerator<T>
97+
where TEnumerable : struct, IStructEnumerable<T, TEnumerator>
98+
{
99+
using (var enumerator = enumerable.GetStructEnumerator())
100+
{
101+
while (enumerator.MoveNext())
102+
{
103+
}
104+
}
105+
106+
}
107+
108+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109+
public static void ForEachWithTryFinally<T, TEnumerator, TEnumerable>(ref TEnumerable enumerable,
110+
Func<TEnumerable, IStructEnumerable<T, TEnumerator>> _)
111+
where TEnumerator : struct, IEnumerator<T>
112+
where TEnumerable : struct, IStructEnumerable<T, TEnumerator>
113+
{
114+
var enumerator = enumerable.GetStructEnumerator();
115+
try
116+
{
117+
while (enumerator.MoveNext())
118+
{
119+
}
120+
}
121+
finally
122+
{
123+
enumerator.Dispose();
124+
125+
}
126+
}
127+
128+
public struct Reader<T>
129+
{
130+
public readonly T Value;
131+
132+
public Reader(T value)
133+
{
134+
Value = value;
135+
}
136+
}
137+
138+
private sealed class Class
139+
{
140+
public readonly int Integer;
141+
public Class(int integer)
142+
{
143+
Integer = integer;
144+
}
145+
}
146+
147+
private readonly struct Struct
148+
{
149+
public readonly int Integer;
150+
public Struct(int integer)
151+
{
152+
Integer = integer;
153+
}
154+
}
155+
}
156+
157+
}

0 commit comments

Comments
 (0)