Skip to content

Commit 0fae33b

Browse files
committed
[Benchmark] rework some benchmark.
1 parent 2c2f0b7 commit 0fae33b

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

src/StructLinq.Benchmark/Select.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ namespace StructLinq.Benchmark
1414
//```
1515
//| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
1616
//|--------------- |---------:|---------:|---------:|------:|--------:|------:|------:|------:|----------:|
17-
//| SysSelect | 66.87 us | 1.280 us | 1.524 us | 1.00 | 0.00 | - | - | - | 88 B |
18-
//| DelegateSelect | 30.33 us | 0.606 us | 0.744 us | 0.45 | 0.02 | - | - | - | - |
19-
//| StructSelect | 19.63 us | 0.213 us | 0.189 us | 0.30 | 0.01 | - | - | - | - |
20-
//| ConvertSelect | 69.85 us | 1.508 us | 1.411 us | 1.05 | 0.03 | - | - | - | 40 B |
17+
//| SysSelect | 60.40 us | 0.705 us | 0.588 us | 1.00 | 0.00 | - | - | - | 88 B |
18+
//| DelegateSelect | 31.94 us | 0.623 us | 0.788 us | 0.53 | 0.01 | - | - | - | - |
19+
//| StructSelect | 19.14 us | 0.071 us | 0.066 us | 0.32 | 0.00 | - | - | - | - |
20+
//| ConvertSelect | 44.31 us | 1.078 us | 0.956 us | 0.73 | 0.02 | - | - | - | 40 B |
21+
2122

2223
[MemoryDiagnoser, DisassemblyDiagnoser(recursiveDepth: 4)]
2324
public class Select
@@ -30,13 +31,7 @@ public Select()
3031
[Benchmark(Baseline = true)]
3132
public double SysSelect()
3233
{
33-
double sum = 0;
34-
var enumerable = Enumerable.Range(0, Count).Select(x=> x * 2.0);
35-
foreach (var i in enumerable)
36-
{
37-
sum+= i;
38-
}
39-
return sum;
34+
return Enumerable.Range(0, Count).Select(x=> x * 2.0).Sum();
4035
}
4136

4237
[Benchmark]
@@ -60,9 +55,10 @@ public double StructSelect()
6055
[Benchmark]
6156
public double ConvertSelect()
6257
{
58+
var multFunction = new MultFunction();
6359
return Enumerable.Range(0, Count)
6460
.ToStructEnumerable()
65-
.Select(x=> x * 2.0, x => x)
61+
.Select(ref multFunction, x => x, x=> x)
6662
.Sum(x=>x);
6763
}
6864
}

src/StructLinq.Benchmark/Sum.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
using System.Linq;
32
using BenchmarkDotNet.Attributes;
4-
using StructLinq.IEnumerable;
5-
using StructLinq.Range;
63

74
namespace StructLinq.Benchmark
85
{
@@ -17,24 +14,18 @@ namespace StructLinq.Benchmark
1714
//```
1815
//| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
1916
//|----------- |----------:|----------:|----------:|------:|--------:|------:|------:|------:|----------:|
20-
//| ForSum | 3.116 us | 0.0663 us | 0.1161 us | 1.00 | 0.00 | - | - | - | - |
21-
//| SysSum | 45.478 us | 0.9326 us | 1.3375 us | 14.52 | 0.73 | - | - | - | 40 B |
22-
//| StructSum | 3.044 us | 0.0597 us | 0.0687 us | 0.98 | 0.04 | - | - | - | - |
23-
//| ConvertSum | 44.414 us | 0.6786 us | 0.6348 us | 14.17 | 0.67 | - | - | - | 41 B |
17+
//| ForSum | 3.004 us | 0.0598 us | 0.1000 us | 1.00 | 0.00 | - | - | - | - |
18+
//| SysSum | 40.905 us | 0.5443 us | 0.5092 us | 13.38 | 0.53 | - | - | - | 40 B |
19+
//| StructSum | 2.969 us | 0.0390 us | 0.0365 us | 0.97 | 0.03 | - | - | - | - |
20+
//| ConvertSum | 40.720 us | 0.5673 us | 0.5306 us | 13.32 | 0.58 | - | - | - | 40 B |
2421

2522

2623
[MemoryDiagnoser, DisassemblyDiagnoser(recursiveDepth: 4)]
2724
public class Sum
2825
{
29-
private readonly IEnumerable<int> sysRange;
30-
private readonly IStructEnumerable<int, GenericEnumerator<int>> convertRange;
3126
private const int Count = 10000;
32-
private readonly IStructEnumerable<int, RangeEnumerator> structRange;
3327
public Sum()
3428
{
35-
sysRange = Enumerable.Range(0, Count);
36-
convertRange = Enumerable.Range(0, Count).ToStructEnumerable();
37-
structRange = StructEnumerable.Range(0, Count);
3829
}
3930

4031

@@ -52,19 +43,19 @@ public int ForSum()
5243
[Benchmark]
5344
public int SysSum()
5445
{
55-
return sysRange.Sum();
46+
return Enumerable.Range(0, Count).Sum();
5647
}
5748

5849
[Benchmark]
5950
public int StructSum()
6051
{
61-
return structRange.Sum();
52+
return StructEnumerable.Range(0, Count).Sum(x=>x);
6253
}
6354

6455
[Benchmark]
6556
public int ConvertSum()
6657
{
67-
return convertRange.Sum();
58+
return Enumerable.Range(0, Count).ToStructEnumerable().Sum(x=>x);
6859
}
6960

7061
}

0 commit comments

Comments
 (0)