Skip to content

Commit a480d0b

Browse files
committed
[Benchmark] housekeeping
1 parent b72acb5 commit a480d0b

File tree

11 files changed

+46
-23
lines changed

11 files changed

+46
-23
lines changed

src/StructLinq.Benchmark/ArrayOfBigStructSum.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Runtime.CompilerServices;
34
using BenchmarkDotNet.Attributes;
45

56
namespace StructLinq.Benchmark
@@ -65,6 +66,7 @@ public int ZeroAllocRefStructSum()
6566

6667
internal struct StructContainerSelect : IFunction<StructContainer, int>
6768
{
69+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6870
public readonly int Eval(StructContainer element)
6971
{
7072
return element.Element;
@@ -73,6 +75,7 @@ public readonly int Eval(StructContainer element)
7375

7476
internal struct InStructContainerSelect : IInFunction<StructContainer, int>
7577
{
78+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7679
public readonly int Eval(in StructContainer element)
7780
{
7881
return element.Element;

src/StructLinq.Benchmark/ArrayOfIntSum.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using BenchmarkDotNet.Attributes;
4+
using StructLinq.Array;
45
using Enumerable = System.Linq.Enumerable;
56

67
namespace StructLinq.Benchmark
@@ -20,20 +21,27 @@ public ArrayOfIntSum()
2021
public int Handmaded()
2122
{
2223
int sum = 0;
23-
foreach (var i in array)
24+
var enumerable = array;
25+
foreach (var i in enumerable)
2426
{
25-
sum += array[i];
27+
sum += enumerable[i];
2628
}
2729
return sum;
2830
}
31+
[Benchmark]
32+
public int EnumerableLINQ() => sysArray.Sum();
33+
2934
[Benchmark(Baseline = true)]
30-
public int LINQ() => sysArray.Sum();
35+
public int ArrayLINQ() => array.Sum();
3136

3237
[Benchmark]
3338
public int StructLinqZeroAlloc() => array.ToStructEnumerable().Sum(x=>x);
3439

3540
[Benchmark]
3641
public int StructLinq() => array.ToStructEnumerable().Sum();
3742

43+
[Benchmark]
44+
public int StructLinqZeroAlloc2() => array.ToStructEnumerable().Sum(x=> (IStructEnumerable<int, ArrayStructEnumerator<int>>)x);
45+
3846
}
3947
}

src/StructLinq.Benchmark/Dictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace StructLinq.Benchmark
77
{
88
[DisassemblyDiagnoser(4), MemoryDiagnoser]
99
[SimpleJob(RuntimeMoniker.Net48, baseline:true)]
10-
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
10+
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
1111
public class Dictionary
1212
{
1313

src/StructLinq.Benchmark/ForEachOnListWithSelect.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Runtime.CompilerServices;
34
using BenchmarkDotNet.Attributes;
45

56
namespace StructLinq.Benchmark
@@ -74,6 +75,7 @@ public int StructLinqWithStructFuncAsEnumerable()
7475

7576
public readonly struct Mult2 : IFunction<int, int>
7677
{
78+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7779
public int Eval(int element)
7880
{
7981
return element * 2;

src/StructLinq.Benchmark/Max.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq;
22
using BenchmarkDotNet.Attributes;
3+
using StructLinq.Range;
34

45
namespace StructLinq.Benchmark
56
{
@@ -9,41 +10,49 @@ public class Max
910
private const int Count = 10000;
1011

1112
[Benchmark(Baseline = true)]
12-
public int RawMax()
13+
public int Handmaded()
1314
{
14-
var max = int.MaxValue;
15+
var max = int.MinValue;
1516
for (var index = 0; index < Count; index++)
1617
{
17-
if (index < max)
18+
if (index > max)
1819
max = index;
1920
}
2021

2122
return max;
2223
}
2324

2425
[Benchmark]
25-
public int SysMax()
26+
public int LINQ()
2627
{
27-
return Enumerable.Range(0, Count).Min();
28+
return Enumerable.Range(0, Count).Max();
2829
}
2930

3031
[Benchmark]
31-
public int StructMax()
32+
public int StructLINQ()
3233
{
33-
return StructEnumerable.Range(0, Count).Min();
34+
return StructEnumerable.Range(0, Count).Max();
3435
}
3536

37+
38+
[Benchmark]
39+
public int ZeroAllocStructLINQ()
40+
{
41+
return StructEnumerable.Range(0, Count).Max(x=> x);
42+
}
43+
44+
3645
[Benchmark]
37-
public int ZeroAllocStructMax()
46+
public int ZeroAllocStructLINQOnEnumerable()
3847
{
39-
return StructEnumerable.Range(0, Count).Min(x=>x);
48+
return StructEnumerable.Range(0, Count).Max(x=>(IStructEnumerable<int, RangeEnumerator>)x);
4049
}
4150

4251

4352
[Benchmark]
44-
public int ConvertMax()
53+
public int ConvertMin()
4554
{
46-
return Enumerable.Range(0, Count).ToStructEnumerable().Min();
55+
return Enumerable.Range(0, Count).ToStructEnumerable().Max();
4756
}
4857

4958
}

src/StructLinq.Benchmark/OrderByArrayOfInt.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime.CompilerServices;
45
using BenchmarkDotNet.Attributes;
56

67
namespace StructLinq.Benchmark
@@ -92,6 +93,7 @@ public int StructLinqOrderByZeroAlloc()
9293

9394
public struct Identity<T> : IFunction<T, T>
9495
{
96+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9597
public T Eval(T element)
9698
{
9799
return element;

src/StructLinq.Benchmark/RangeWhereSelectSum.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Linq;
2+
using System.Runtime.CompilerServices;
23
using BenchmarkDotNet.Attributes;
34

45
namespace StructLinq.Benchmark
@@ -53,6 +54,7 @@ public int StructRangeWhereSelectSum()
5354

5455
struct WherePredicate : IFunction<int, bool>
5556
{
57+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5658
public readonly bool Eval(int element)
5759
{
5860
return (element & 1) == 0;
@@ -61,6 +63,7 @@ public readonly bool Eval(int element)
6163

6264
struct SelectFunction : IFunction<int, int>
6365
{
66+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6467
public readonly int Eval(int element)
6568
{
6669
return element * 2;

src/StructLinq.Benchmark/Sum.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Runtime.CompilerServices;
34
using BenchmarkDotNet.Attributes;
45

56
namespace StructLinq.Benchmark
@@ -62,6 +63,7 @@ public int ConvertSum()
6263

6364
public struct IntComparer : IComparer<int>
6465
{
66+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6567
public int Compare(int x, int y)
6668
{
6769
return x - y;

src/StructLinq.Benchmark/SumOnList.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using System.Runtime.CompilerServices;
43
using BenchmarkDotNet.Attributes;
54

65
namespace StructLinq.Benchmark

src/StructLinq.Benchmark/ToListComparison.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
namespace StructLinq.Benchmark
1010
{
11-
1211
[DisassemblyDiagnoser(4), MemoryDiagnoser]
1312
[SimpleJob(RuntimeMoniker.Net48)]
14-
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
13+
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
1514
public class ToListComparison
1615
{
1716
private int[] enumerable;

0 commit comments

Comments
 (0)