Skip to content

Commit 1b7ceb7

Browse files
committed
Benchmark: Update benchmark.
1 parent 0fae33b commit 1b7ceb7

11 files changed

+262
-471
lines changed

src/StructLinq.Benchmark/ArrayClassSum.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/StructLinq.Benchmark/ArrayStructSum.cs renamed to src/StructLinq.Benchmark/ArrayOfBigStructSum.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
using System.Linq;
32
using BenchmarkDotNet.Attributes;
4-
using StructLinq.Array;
5-
using StructLinq.Select;
63

74
namespace StructLinq.Benchmark
85
{
6+
7+
//BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
8+
//Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
9+
//.NET Core SDK=3.1.101
10+
//[Host] : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
11+
//DefaultJob : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
12+
13+
14+
//```
15+
//| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
16+
//|----------------- |-----------:|---------:|---------:|------:|-------:|------:|------:|----------:|
17+
//| SysSum | 613.5 ns | 2.22 ns | 2.08 ns | 0.09 | - | - | - | - |
18+
//| SysEnumerableSum | 6,813.1 ns | 26.47 ns | 23.46 ns | 1.00 | 0.0076 | - | - | 48 B |
19+
//| StructSum | 3,132.6 ns | 14.93 ns | 13.96 ns | 0.46 | - | - | - | - |
20+
921
[MemoryDiagnoser]
10-
public class ArrayStructSum
22+
public class ArrayOfBigStructSum
1123
{
12-
private readonly IEnumerable<int> sysArray;
1324
private const int Count = 1000;
1425
private readonly StructContainer[] array;
15-
private readonly IStructEnumerable<int, SelectEnumerator<StructContainer, int, ArrayStructEnumerator<StructContainer>, StructContainerSelect>> safeStructArray;
16-
private readonly IStructEnumerable<int, SelectEnumerator<StructContainer, int, ArrayStructEnumerator<StructContainer>, StructFunction<StructContainer, int>>> convertArray;
17-
public ArrayStructSum()
26+
public ArrayOfBigStructSum()
1827
{
1928
array = Enumerable.Range(0, Count).Select(x => StructContainer.Create(x)).ToArray();
20-
var @select = new StructContainerSelect();
21-
sysArray = array.Select(x => x.Element);
22-
convertArray = array.ToStructEnumerable().Select(x => x.Element, x => x);
23-
safeStructArray = array.ToStructEnumerable().Select(ref select, x=>x, x => x);
2429
}
2530
[Benchmark]
2631
public int SysSum()
@@ -33,13 +38,16 @@ public int SysSum()
3338
return sum;
3439
}
3540
[Benchmark(Baseline = true)]
36-
public int SysEnumerableSum() => sysArray.Sum();
41+
public int SysEnumerableSum() => array.Select(x=> x.Element).Sum();
3742

3843
[Benchmark]
39-
public int ConvertSum() => convertArray.Sum();
40-
41-
[Benchmark]
42-
public int SafeStructSum() => safeStructArray.Sum();
44+
public int StructSum()
45+
{
46+
var @select = new StructContainerSelect();
47+
return array.ToStructEnumerable()
48+
.Select(ref @select, x=>x, x=>x)
49+
.Sum(x=>x);
50+
}
4351
}
4452

4553

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Linq;
2+
using BenchmarkDotNet.Attributes;
3+
4+
namespace StructLinq.Benchmark
5+
{
6+
//BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
7+
//Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
8+
//.NET Core SDK=3.1.101
9+
//[Host] : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
10+
//DefaultJob : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
11+
12+
13+
//```
14+
//| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
15+
//|----------------- |-----------:|---------:|---------:|------:|-------:|------:|------:|----------:|
16+
//| SysSum | 574.0 ns | 2.93 ns | 2.74 ns | 0.09 | - | - | - | - |
17+
//| SysEnumerableSum | 6,617.1 ns | 11.55 ns | 10.24 ns | 1.00 | 0.0076 | - | - | 48 B |
18+
//| StructSum | 1,736.7 ns | 3.68 ns | 3.44 ns | 0.26 | - | - | - | - |
19+
20+
21+
[MemoryDiagnoser]
22+
public class ArrayOfClassSum
23+
{
24+
private const int Count = 1000;
25+
private readonly Container[] array;
26+
public ArrayOfClassSum()
27+
{
28+
array = Enumerable.Range(0, Count).Select(x => new Container(x)).ToArray();
29+
}
30+
[Benchmark]
31+
public int SysSum()
32+
{
33+
int sum = 0;
34+
for (int i = 0; i < Count; i++)
35+
{
36+
sum += array[i].Element;
37+
}
38+
return sum;
39+
}
40+
[Benchmark(Baseline = true)]
41+
public int SysEnumerableSum() => array.Select(x => x.Element).Sum();
42+
43+
[Benchmark]
44+
public int StructSum()
45+
{
46+
var @select = new ContainerSelect();
47+
return array.ToStructEnumerable()
48+
.Select(ref @select, x=>x, x=>x)
49+
.Sum(x => x);
50+
}
51+
}
52+
53+
54+
internal class Container
55+
{
56+
public readonly int Element;
57+
public Container(int element)
58+
{
59+
Element = element;
60+
}
61+
}
62+
63+
64+
internal struct ContainerSelect : IFunction<Container, int>
65+
{
66+
public readonly int Eval(Container element)
67+
{
68+
return element.Element;
69+
}
70+
}
71+
72+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
using BenchmarkDotNet.Attributes;
3+
using Enumerable = System.Linq.Enumerable;
4+
5+
namespace StructLinq.Benchmark
6+
{
7+
8+
//BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
9+
//Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
10+
//.NET Core SDK=3.1.101
11+
//[Host] : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
12+
//DefaultJob : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
13+
14+
15+
//```
16+
//| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
17+
//|----------------- |-----------:|---------:|---------:|------:|-------:|------:|------:|----------:|
18+
//| SysSum | 589.0 ns | 2.02 ns | 1.79 ns | 0.14 | - | - | - | - |
19+
//| SysEnumerableSum | 4,130.5 ns | 12.05 ns | 11.28 ns | 1.00 | 0.0076 | - | - | 32 B |
20+
//| ConvertSum | 3,963.7 ns | 8.69 ns | 7.25 ns | 0.96 | 0.0076 | - | - | 32 B |
21+
//| StructSum | 589.3 ns | 1.03 ns | 0.91 ns | 0.14 | - | - | - | - |
22+
23+
[MemoryDiagnoser]
24+
public class ArrayOfIntSum
25+
{
26+
private readonly IEnumerable<int> sysArray;
27+
private readonly int Count = 1000;
28+
private readonly int[] array;
29+
public ArrayOfIntSum()
30+
{
31+
sysArray = Enumerable.ToArray(Enumerable.Range(0,Count));
32+
array = Enumerable.ToArray(Enumerable.Range(0, Count));
33+
}
34+
[Benchmark]
35+
public int SysSum()
36+
{
37+
int sum = 0;
38+
foreach (var i in array)
39+
{
40+
sum += array[i];
41+
}
42+
return sum;
43+
}
44+
[Benchmark(Baseline = true)]
45+
public int SysEnumerableSum() => Enumerable.Sum(sysArray);
46+
47+
[Benchmark]
48+
public int ConvertSum() => sysArray.ToStructEnumerable().Sum(x=>x);
49+
50+
[Benchmark]
51+
public int StructSum() => array.ToStructEnumerable().Sum(x=>x);
52+
}
53+
}

src/StructLinq.Benchmark/ArrayOfStruct.cs

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)