Skip to content

Commit f80fda2

Browse files
committed
[Array] Add benchmark on visitor.
1 parent 6010213 commit f80fda2

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## VisitorOnArray
2+
3+
### Source
4+
[VisitorOnArray.cs](../../src/StructLinq.Benchmark/VisitorOnArray.cs)
5+
6+
### Results:
7+
``` ini
8+
9+
BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19043.1081 (21H1/May2021Update)
10+
Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
11+
.NET SDK=5.0.301
12+
[Host] : .NET 5.0.7 (5.0.721.25508), X64 RyuJIT
13+
DefaultJob : .NET 5.0.7 (5.0.721.25508), X64 RyuJIT
14+
15+
16+
```
17+
| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated |
18+
|--------------------- |---------:|----------:|----------:|------:|------:|------:|----------:|
19+
| Sum | 2.704 μs | 0.0385 μs | 0.0301 μs | - | - | - | - |
20+
| Visitor | 1.870 μs | 0.0027 μs | 0.0023 μs | - | - | - | - |
21+
| VisitorOnTake | 1.502 μs | 0.0037 μs | 0.0032 μs | - | - | - | - |
22+
| VisitorOnSkip | 1.838 μs | 0.0066 μs | 0.0062 μs | - | - | - | - |
23+
| VisitorOnSkipAndTake | 1.467 μs | 0.0046 μs | 0.0040 μs | - | - | - | - |
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System.Linq;
2+
using System.Runtime.CompilerServices;
3+
using BenchmarkDotNet.Attributes;
4+
5+
namespace StructLinq.Benchmark
6+
{
7+
[MemoryDiagnoser]
8+
public class VisitorOnArray
9+
{
10+
private const int Count = 1_000;
11+
private readonly int[] array;
12+
13+
public VisitorOnArray()
14+
{
15+
array = Enumerable.Range(0, Count).ToArray();
16+
}
17+
18+
[Benchmark]
19+
public int Sum()
20+
{
21+
return array.ToStructEnumerable().Sum(x=>x);
22+
}
23+
24+
[Benchmark]
25+
public int Visitor()
26+
{
27+
var visitor = new SumVisitor(0);
28+
array.ToStructEnumerable().Visit(ref visitor);
29+
return visitor.sum;
30+
}
31+
32+
[Benchmark]
33+
public int VisitorOnTake()
34+
{
35+
var visitor = new SumVisitor(0);
36+
array.ToStructEnumerable().Take(900, x=>x).Visit(ref visitor);
37+
return visitor.sum;
38+
}
39+
40+
[Benchmark]
41+
public int VisitorOnSkip()
42+
{
43+
var visitor = new SumVisitor(0);
44+
array.ToStructEnumerable().Skip(100, x=>x).Visit(ref visitor);
45+
return visitor.sum;
46+
}
47+
48+
[Benchmark]
49+
public int VisitorOnSkipAndTake()
50+
{
51+
var visitor = new SumVisitor(0);
52+
array.ToStructEnumerable().Take(900, x=>x).Skip(100, x => x).Visit(ref visitor);
53+
return visitor.sum;
54+
}
55+
56+
57+
private struct SumVisitor : IVisitor<int>
58+
{
59+
public int sum;
60+
61+
public SumVisitor(int sum)
62+
{
63+
this.sum = sum;
64+
}
65+
66+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
67+
public bool Visit(int input)
68+
{
69+
sum += input;
70+
return true;
71+
}
72+
}
73+
}
74+
}

src/StructLinq/Array/ArrayEnumerable.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.CompilerServices;
2+
using System.Runtime.InteropServices;
23
using StructLinq.Utils;
34

45
namespace StructLinq.Array
@@ -20,7 +21,7 @@ public ArrayEnumerable(T[] array, int start, int length)
2021
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2122
public readonly ArrayStructEnumerator<T> GetEnumerator()
2223
{
23-
return new ArrayStructEnumerator<T>(array, start, Count);
24+
return new(array, start, Count);
2425
}
2526

2627
public readonly int Count
@@ -34,7 +35,7 @@ public void Slice(uint start, uint? length)
3435
{
3536
checked
3637
{
37-
this.start = (int)start + this.start;
38+
this.start = MathHelpers.Min((int)start + this.start, this.length);
3839
if (length.HasValue)
3940
this.length = MathHelpers.Min((int)length.Value + this.start, this.length);
4041
}
@@ -47,14 +48,14 @@ public readonly object Clone()
4748
}
4849

4950
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50-
public T Get(int i)
51+
public readonly T Get(int i)
5152
{
5253
return array[start + i];
5354
}
5455

5556

5657
[MethodImpl(MethodImplOptions.AggressiveInlining)]
57-
public VisitStatus Visit<TVisitor>(ref TVisitor visitor)
58+
public readonly VisitStatus Visit<TVisitor>(ref TVisitor visitor)
5859
where TVisitor : IVisitor<T>
5960
{
6061
unchecked

0 commit comments

Comments
 (0)