Skip to content

Commit 3a8c586

Browse files
committed
[Benchmark] Add benchmarks on Array.
1 parent 81a08e2 commit 3a8c586

File tree

4 files changed

+253
-16
lines changed

4 files changed

+253
-16
lines changed

src/StructLinq.Benchmark/ArrayClassSum.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public class ArrayClassSum
1212
private readonly IEnumerable<int> sysArray;
1313
public int Count = 1000;
1414
private readonly Container[] array;
15-
private readonly ITypedEnumerable<int, SelectEnumerator<Container, int, ArrayStructEnumerator<Container>, Select>> safeStructArray;
15+
private readonly ITypedEnumerable<int, SelectEnumerator<Container, int, ArrayStructEnumerator<Container>, ContainerSelect>> safeStructArray;
1616
private ITypedEnumerable<int, SelectEnumerator<Container, int, ArrayStructEnumerator<Container>, StructFunction<Container, int>>> convertArray;
17-
private Select select;
17+
private ContainerSelect select;
1818
public ArrayClassSum()
1919
{
20-
array = Enumerable.Range(0, Count).Select(x=> new Container(x)).ToArray();
21-
select = new Select();
22-
sysArray = array.Select(x=> x.Element);
20+
array = Enumerable.Range(0, Count).Select(x => new Container(x)).ToArray();
21+
select = new ContainerSelect();
22+
sysArray = array.Select(x => x.Element);
2323
convertArray = array.ToTypedEnumerable().Select(x => x.Element);
2424
safeStructArray = array.ToTypedEnumerable().Select(ref select, Id<int>.Value);
2525
}
@@ -41,22 +41,25 @@ public int SysSum()
4141

4242
[Benchmark]
4343
public int SafeStructSum() => safeStructArray.Sum();
44+
}
45+
4446

45-
private sealed class Container
47+
internal class Container
48+
{
49+
public readonly int Element;
50+
public Container(int element)
4651
{
47-
public readonly int Element;
48-
public Container(int element)
49-
{
50-
Element = element;
51-
}
52+
Element = element;
5253
}
54+
}
5355

54-
private struct Select : IFunction<Container, int>
55-
{
56+
57+
internal struct ContainerSelect : IFunction<Container, int>
58+
{
5659
public int Eval(in Container element)
57-
{
58-
return element.Element;
59-
}
60+
{
61+
return element.Element;
6062
}
6163
}
64+
6265
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using BenchmarkDotNet.Attributes;
4+
using StructLinq.Array;
5+
using StructLinq.Select;
6+
7+
namespace StructLinq.Benchmark
8+
{
9+
[MemoryDiagnoser]
10+
public class ArrayReadonlyStructSum
11+
{
12+
private readonly IEnumerable<int> sysArray;
13+
public int Count = 1000;
14+
private readonly ReadonlyStructContainer[] array;
15+
private readonly ITypedEnumerable<int, SelectEnumerator<ReadonlyStructContainer, int, ArrayStructEnumerator<ReadonlyStructContainer>, ReadonlyStructContainerSelect>> safeStructArray;
16+
private ITypedEnumerable<int, SelectEnumerator<ReadonlyStructContainer, int, ArrayStructEnumerator<ReadonlyStructContainer>, StructFunction<ReadonlyStructContainer, int>>> convertArray;
17+
private ReadonlyStructContainerSelect select;
18+
public ArrayReadonlyStructSum()
19+
{
20+
array = Enumerable.Range(0, Count).Select(x => ReadonlyStructContainer.Create(x)).ToArray();
21+
select = new ReadonlyStructContainerSelect();
22+
sysArray = array.Select(x => x.Element);
23+
convertArray = array.ToTypedEnumerable().Select(x => x.Element);
24+
safeStructArray = array.ToTypedEnumerable().Select(ref select, Id<int>.Value);
25+
}
26+
[Benchmark]
27+
public int SysSum()
28+
{
29+
int sum = 0;
30+
for (int i = 0; i < Count; i++)
31+
{
32+
sum += array[i].Element;
33+
}
34+
return sum;
35+
}
36+
[Benchmark(Baseline = true)]
37+
public int SysEnumerableSum() => sysArray.Sum();
38+
39+
[Benchmark]
40+
public int ConvertSum() => convertArray.Sum();
41+
42+
[Benchmark]
43+
public int SafeStructSum() => safeStructArray.Sum();
44+
}
45+
46+
internal struct ReadonlyStructContainerSelect : IFunction<ReadonlyStructContainer, int>
47+
{
48+
public int Eval(in ReadonlyStructContainer element)
49+
{
50+
return element.Element;
51+
}
52+
}
53+
54+
55+
internal readonly struct ReadonlyStructContainer
56+
{
57+
public readonly int Element;
58+
public readonly int Element1;
59+
public readonly int Element2;
60+
public readonly int Element3;
61+
public readonly int Element4;
62+
public readonly int Element5;
63+
public readonly int Element6;
64+
public readonly int Element7;
65+
public readonly int Element8;
66+
public ReadonlyStructContainer(int element, int element1, int element2, int element3, int element4, int element5, int element6, int element7, int element8)
67+
{
68+
Element = element;
69+
Element1 = element1;
70+
Element2 = element2;
71+
Element3 = element3;
72+
Element4 = element4;
73+
Element5 = element5;
74+
Element6 = element6;
75+
Element7 = element7;
76+
Element8 = element8;
77+
}
78+
public static ReadonlyStructContainer Create(int element)
79+
{
80+
return new ReadonlyStructContainer(element, element, element, element, element, element, element, element, element);
81+
}
82+
}
83+
84+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using BenchmarkDotNet.Attributes;
4+
using StructLinq.Array;
5+
using StructLinq.Select;
6+
7+
namespace StructLinq.Benchmark
8+
{
9+
[MemoryDiagnoser]
10+
public class ArraySealedClassSum
11+
{
12+
private readonly IEnumerable<int> sysArray;
13+
public int Count = 1000;
14+
private readonly SealedContainer[] array;
15+
private readonly ITypedEnumerable<int, SelectEnumerator<SealedContainer, int, ArrayStructEnumerator<SealedContainer>, SealedContainerSelect>> safeStructArray;
16+
private ITypedEnumerable<int, SelectEnumerator<SealedContainer, int, ArrayStructEnumerator<SealedContainer>, StructFunction<SealedContainer, int>>> convertArray;
17+
private SealedContainerSelect select;
18+
public ArraySealedClassSum()
19+
{
20+
array = Enumerable.Range(0, Count).Select(x=> new SealedContainer(x)).ToArray();
21+
select = new SealedContainerSelect();
22+
sysArray = array.Select(x=> x.Element);
23+
convertArray = array.ToTypedEnumerable().Select(x => x.Element);
24+
safeStructArray = array.ToTypedEnumerable().Select(ref select, Id<int>.Value);
25+
}
26+
[Benchmark]
27+
public int SysSum()
28+
{
29+
int sum = 0;
30+
for (int i = 0; i < Count; i++)
31+
{
32+
sum += array[i].Element;
33+
}
34+
return sum;
35+
}
36+
[Benchmark(Baseline = true)]
37+
public int SysEnumerableSum() => sysArray.Sum();
38+
39+
[Benchmark]
40+
public int ConvertSum() => convertArray.Sum();
41+
42+
[Benchmark]
43+
public int SafeStructSum() => safeStructArray.Sum();
44+
}
45+
46+
internal struct SealedContainerSelect : IFunction<SealedContainer, int>
47+
{
48+
public int Eval(in SealedContainer element)
49+
{
50+
return element.Element;
51+
}
52+
}
53+
54+
55+
internal sealed class SealedContainer
56+
{
57+
public readonly int Element;
58+
public SealedContainer(int element)
59+
{
60+
Element = element;
61+
}
62+
}
63+
64+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using BenchmarkDotNet.Attributes;
4+
using StructLinq.Array;
5+
using StructLinq.Select;
6+
7+
namespace StructLinq.Benchmark
8+
{
9+
[MemoryDiagnoser]
10+
public class ArrayStructSum
11+
{
12+
private readonly IEnumerable<int> sysArray;
13+
public int Count = 1000;
14+
private readonly StructContainer[] array;
15+
private readonly ITypedEnumerable<int, SelectEnumerator<StructContainer, int, ArrayStructEnumerator<StructContainer>, StructContainerSelect>> safeStructArray;
16+
private ITypedEnumerable<int, SelectEnumerator<StructContainer, int, ArrayStructEnumerator<StructContainer>, StructFunction<StructContainer, int>>> convertArray;
17+
private StructContainerSelect select;
18+
public ArrayStructSum()
19+
{
20+
array = Enumerable.Range(0, Count).Select(x => StructContainer.Create(x)).ToArray();
21+
select = new StructContainerSelect();
22+
sysArray = array.Select(x => x.Element);
23+
convertArray = array.ToTypedEnumerable().Select(x => x.Element);
24+
safeStructArray = array.ToTypedEnumerable().Select(ref select, Id<int>.Value);
25+
}
26+
[Benchmark]
27+
public int SysSum()
28+
{
29+
int sum = 0;
30+
for (int i = 0; i < Count; i++)
31+
{
32+
sum += array[i].Element;
33+
}
34+
return sum;
35+
}
36+
[Benchmark(Baseline = true)]
37+
public int SysEnumerableSum() => sysArray.Sum();
38+
39+
[Benchmark]
40+
public int ConvertSum() => convertArray.Sum();
41+
42+
[Benchmark]
43+
public int SafeStructSum() => safeStructArray.Sum();
44+
}
45+
46+
47+
internal struct StructContainerSelect : IFunction<StructContainer, int>
48+
{
49+
public int Eval(in StructContainer element)
50+
{
51+
return element.Element;
52+
}
53+
}
54+
55+
56+
internal struct StructContainer
57+
{
58+
public readonly int Element;
59+
public readonly int Element1;
60+
public readonly int Element2;
61+
public readonly int Element3;
62+
public readonly int Element4;
63+
public readonly int Element5;
64+
public readonly int Element6;
65+
public readonly int Element7;
66+
public readonly int Element8;
67+
public StructContainer(int element, int element1, int element2, int element3, int element4, int element5, int element6, int element7, int element8)
68+
{
69+
Element = element;
70+
Element1 = element1;
71+
Element2 = element2;
72+
Element3 = element3;
73+
Element4 = element4;
74+
Element5 = element5;
75+
Element6 = element6;
76+
Element7 = element7;
77+
Element8 = element8;
78+
}
79+
public static StructContainer Create(int element)
80+
{
81+
return new StructContainer(element, element, element, element, element, element, element, element, element);
82+
}
83+
}
84+
85+
86+
}

0 commit comments

Comments
 (0)