Skip to content

Commit 0086f22

Browse files
committed
[ForEach] Add for loop in bench.
1 parent c847ac6 commit 0086f22

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

src/StructLinq.Benchmark/ForEachDuckTyping.cs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,61 @@
55
namespace StructLinq.Benchmark
66
{
77

8-
//BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
9-
//Intel Core i7-8750H CPU 2.20GHz(Coffee Lake), 1 CPU, 12 logical and 6 physical cores
10-
//.NET Core SDK = 3.1.201
8+
//``` ini
119

12-
//[Host] : .NET Core 3.1.3 (CoreCLR 4.700.20.11803, CoreFX 4.700.20.12001), X64 RyuJIT
13-
//DefaultJob : .NET Core 3.1.3 (CoreCLR 4.700.20.11803, CoreFX 4.700.20.12001), X64 RyuJIT
10+
//BenchmarkDotNet=v0.12.0, OS=Windows 10.0.19041
11+
//Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
12+
//.NET Core SDK=3.1.402
13+
//[Host] : .NET Core 3.1.8 (CoreCLR 4.700.20.41105, CoreFX 4.700.20.41903), X64 RyuJIT
14+
//DefaultJob : .NET Core 3.1.8 (CoreCLR 4.700.20.41105, CoreFX 4.700.20.41903), X64 RyuJIT
1415

1516

1617
//```
1718
//| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
1819
//|-------------------------------------------- |----------:|----------:|----------:|------:|--------:|------:|------:|------:|----------:|
19-
//| ForEachOnArray | 5.217 us | 0.0336 us | 0.0314 us | 1.00 | 0.00 | - | - | - | - |
20-
//| ForEachOnIEnumerable | 39.114 us | 0.2016 us | 0.1886 us | 7.50 | 0.06 | - | - | - | 32 B |
21-
//| ForEachOnArrayStructEnumerable | 5.195 us | 0.0189 us | 0.0176 us | 1.00 | 0.01 | - | - | - | - |
22-
//| ForEachOnArrayStructEnumerableAsIEnumerable | 68.004 us | 0.1789 us | 0.1674 us | 13.04 | 0.09 | - | - | - | 32 B |
20+
//| ForOnArray | 6.264 us | 0.1056 us | 0.0988 us | 1.00 | 0.00 | - | - | - | - |
21+
//| ForEachOnArray | 4.627 us | 0.0907 us | 0.1516 us | 0.74 | 0.02 | - | - | - | - |
22+
//| ForEachOnYieldEnumerable | 51.903 us | 0.4945 us | 0.4626 us | 8.29 | 0.15 | - | - | - | 57 B |
23+
//| ForEachOnIEnumerable | 46.738 us | 0.6210 us | 0.5505 us | 7.46 | 0.15 | - | - | - | 32 B |
24+
//| ForEachOnArrayStructEnumerable | 5.782 us | 0.0675 us | 0.0631 us | 0.92 | 0.02 | - | - | - | - |
25+
//| ForEachOnArrayStructEnumerableAsIEnumerable | 57.834 us | 0.4597 us | 0.4075 us | 9.24 | 0.18 | - | - | - | 64 B |
2326

24-
[MemoryDiagnoser]
27+
[DisassemblyDiagnoser(recursiveDepth: 4), MemoryDiagnoser]
2528
public class ForEachDuckTyping
2629
{
2730
private const int Count = 10_000;
2831
private readonly int[] array;
2932
private readonly IEnumerable<int> sysEnumerable;
30-
private readonly IEnumerable<int> arrayEnumerableAsIEnumerable;
3133

3234
public ForEachDuckTyping()
3335
{
3436
array = Enumerable.Range(0, Count).ToArray();
3537
sysEnumerable = Enumerable.Range(0, Count).ToArray();
36-
arrayEnumerableAsIEnumerable = Enumerable.Range(0, Count).ToArray().ToStructEnumerable().ToEnumerable();
38+
}
39+
40+
private IEnumerable<int> YieldEnumerable()
41+
{
42+
var arrayLength = array.Length;
43+
var tab = array;
44+
for (int i = 0; i < arrayLength; i++)
45+
{
46+
yield return tab[i];
47+
}
3748
}
3849

3950
[Benchmark(Baseline = true)]
51+
public int ForOnArray()
52+
{
53+
var sum = 0;
54+
for (int i = 0; i < array.Length; i++)
55+
{
56+
sum += array[i];
57+
}
58+
return sum;
59+
}
60+
61+
62+
[Benchmark]
4063
public int ForEachOnArray()
4164
{
4265
var sum = 0;
@@ -47,6 +70,17 @@ public int ForEachOnArray()
4770
return sum;
4871
}
4972

73+
[Benchmark]
74+
public int ForEachOnYieldEnumerable()
75+
{
76+
var sum = 0;
77+
foreach (var i in YieldEnumerable())
78+
{
79+
sum += i;
80+
}
81+
return sum;
82+
}
83+
5084
[Benchmark]
5185
public int ForEachOnIEnumerable()
5286
{
@@ -73,7 +107,7 @@ public int ForEachOnArrayStructEnumerable()
73107
public int ForEachOnArrayStructEnumerableAsIEnumerable()
74108
{
75109
var sum = 0;
76-
foreach (var i in arrayEnumerableAsIEnumerable)
110+
foreach (var i in array.ToStructEnumerable().ToEnumerable())
77111
{
78112
sum += i;
79113
}

0 commit comments

Comments
 (0)