-
Notifications
You must be signed in to change notification settings - Fork 272
/
SpanWrapper.cs
62 lines (52 loc) · 1.51 KB
/
SpanWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Linq;
using System;
using System.Runtime.CompilerServices;
namespace Struct
{
[BenchmarkCategory(Categories.Runtime, Categories.JIT)]
public class SpanWrapper
{
private int[] _array;
[GlobalSetup]
public void Setup()
{
_array = Enumerable.Range(0, 10000).ToArray();
}
[Benchmark]
public int BaselineSum()
{
return SumSpan(_array);
}
[Benchmark]
public int WrapperSum()
{
return SumSpanWrapper(new SpanWrapper<int> { Span = _array });
}
[MethodImpl(MethodImplOptions.NoInlining)]
private int SumSpan(ReadOnlySpan<int> span)
{
int sum = 0;
foreach (int val in span)
sum += val;
return sum;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private int SumSpanWrapper(SpanWrapper<int> spanWrapper)
{
int sum = 0;
foreach (int val in spanWrapper)
sum += val;
return sum;
}
}
public ref struct SpanWrapper<T>
{
public ReadOnlySpan<T> Span;
public ReadOnlySpan<T>.Enumerator GetEnumerator() => Span.GetEnumerator();
}
}