-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathConstructors.cs
125 lines (94 loc) · 4.66 KB
/
Constructors.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 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 System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Memory
{
[GenericTypeArguments(typeof(byte))]
[GenericTypeArguments(typeof(string))]
[BenchmarkCategory(Categories.Runtime, Categories.Libraries, Categories.Span)]
public class Constructors<T>
{
private const int Size = 10;
private T[] _nonEmptyArray;
private ArraySegment<T> _arraySegment;
private T _field;
private System.Memory<T> _memory;
private System.ReadOnlyMemory<T> _readOnlyMemory;
public Constructors()
{
_nonEmptyArray = new T[Size];
_arraySegment = new ArraySegment<T>(_nonEmptyArray, 0, Size);
_field = _nonEmptyArray[0];
_memory = new System.Memory<T>(_nonEmptyArray);
_readOnlyMemory = new System.ReadOnlyMemory<T>(_nonEmptyArray);
}
[Benchmark]
public System.Span<T> SpanFromArray() => new System.Span<T>(_nonEmptyArray);
[Benchmark]
public System.ReadOnlySpan<T> ReadOnlySpanFromArray() => new System.ReadOnlySpan<T>(_nonEmptyArray);
[Benchmark]
public System.Span<T> SpanFromArrayStartLength() => new System.Span<T>(_nonEmptyArray, start: 0, length: Size);
[Benchmark]
public System.ReadOnlySpan<T> ReadOnlySpanFromArrayStartLength() => new System.ReadOnlySpan<T>(_nonEmptyArray, start: 0, length: Size);
[Benchmark]
public System.Span<T> SpanFromMemory() => _memory.Span;
[Benchmark]
public System.ReadOnlySpan<T> ReadOnlySpanFromMemory() => _readOnlyMemory.Span;
[Benchmark]
public System.Span<T> SpanImplicitCastFromArray() => _nonEmptyArray;
[Benchmark]
public System.ReadOnlySpan<T> ReadOnlySpanImplicitCastFromArray() => _nonEmptyArray;
[Benchmark]
public System.Span<T> SpanImplicitCastFromArraySegment() => _arraySegment;
[Benchmark]
public System.ReadOnlySpan<T> ReadOnlySpanImplicitCastFromArraySegment() => _arraySegment;
[Benchmark]
public System.ReadOnlySpan<T> ReadOnlySpanImplicitCastFromSpan() => System.Span<T>.Empty;
[Benchmark]
public System.Memory<T> MemoryFromArray() => new System.Memory<T>(_nonEmptyArray);
[Benchmark]
public System.ReadOnlyMemory<T> ReadOnlyMemoryFromArray() => new System.ReadOnlyMemory<T>(_nonEmptyArray);
[Benchmark]
public System.Memory<T> MemoryFromArrayStartLength() => new System.Memory<T>(_nonEmptyArray, start: 0, length: Size);
[Benchmark]
public System.ReadOnlyMemory<T> ReadOnlyMemoryFromArrayStartLength() => new System.ReadOnlyMemory<T>(_nonEmptyArray, start: 0, length: Size);
[Benchmark]
public System.Span<T> ArrayAsSpan() => _nonEmptyArray.AsSpan();
[Benchmark]
public System.Span<T> ArrayAsSpanStartLength() => _nonEmptyArray.AsSpan(start: 0, length: Size);
[Benchmark]
public System.Memory<T> ArrayAsMemory() => _nonEmptyArray.AsMemory();
[Benchmark]
public System.Memory<T> ArrayAsMemoryStartLength() => _nonEmptyArray.AsMemory(start: 0, length: Size);
#if !NETFRAMEWORK // API added in .NET Core 2.1 https://github.com/dotnet/coreclr/issues/16126
[Benchmark]
public System.Span<T> MemoryMarshalCreateSpan() => MemoryMarshal.CreateSpan<T>(ref _field, Size);
[Benchmark]
public System.ReadOnlySpan<T> MemoryMarshalCreateReadOnlySpan() => MemoryMarshal.CreateReadOnlySpan<T>(ref _field, Size);
#endif
}
[GenericTypeArguments(typeof(byte))]
[GenericTypeArguments(typeof(int))]
[BenchmarkCategory(Categories.Runtime, Categories.Libraries, Categories.Span)]
public class Constructors_ValueTypesOnly<T>
{
private const int Size = 10;
private T[] _nonEmptyArray;
private IntPtr _validPointer;
private T _field;
public unsafe Constructors_ValueTypesOnly()
{
_nonEmptyArray = new T[Size];
_field = _nonEmptyArray[0];
_validPointer = (IntPtr)Unsafe.AsPointer(ref _field);
}
[Benchmark]
public unsafe System.Span<T> SpanFromPointerLength() => new System.Span<T>(_validPointer.ToPointer(), Size);
[Benchmark]
public unsafe System.ReadOnlySpan<T> ReadOnlyFromPointerLength() => new System.ReadOnlySpan<T>(_validPointer.ToPointer(), Size);
}
}