-
-
Save swvitaliy/8916f6930ecc1e1c44049c2000745e81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
namespace SpanBench | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//BenchmarkRunner.Run<BenchmarkDemo1>(); | |
//BenchmarkRunner.Run<BenchmarkDemo2>(); | |
//BenchmarkRunner.Run<Benchmark3>(); | |
BenchmarkRunner.Run<BenchmarkStackHeap>(); | |
} | |
} | |
[MemoryDiagnoser] | |
public class BenchmarkDemo1 | |
{ | |
private int[] _myArray; | |
[Params(100, 1000, 10000)] | |
public int Size { get; set; } | |
[GlobalSetup] | |
public void SetUp() | |
{ | |
_myArray = new int[Size]; | |
for (int index = 0; index < Size; index++) | |
{ | |
_myArray[index] = index; | |
} | |
} | |
[Benchmark(Baseline = true)] | |
public int[] Original() | |
{ | |
return _myArray.Skip(Size / 2).Take(Size / 4).ToArray(); | |
} | |
[Benchmark] | |
public int[] ArrayCopy() | |
{ | |
var copy = new int[Size / 4]; | |
Array.Copy(_myArray, Size / 2, copy, 0, Size / 4); | |
return copy; | |
} | |
[Benchmark] | |
public Span<int> Span() | |
{ | |
return _myArray.AsSpan().Slice(Size / 2, Size / 4); | |
} | |
} | |
[MemoryDiagnoser] | |
public class BenchmarkDemo2 | |
{ | |
private static readonly string _dateString = "01 05 1991"; | |
[Benchmark(Baseline = true)] | |
public DateTime Original() | |
{ | |
var day = _dateString.Substring(0, 2); | |
var month = _dateString.Substring(3, 2); | |
var year = _dateString.Substring(6); | |
return new DateTime(int.Parse(year), int.Parse(month), int.Parse(day)); | |
} | |
[Benchmark] | |
public DateTime Span() | |
{ | |
ReadOnlySpan<char> dateSpan = _dateString; | |
var day = dateSpan.Slice(0, 2); | |
var month = dateSpan.Slice(3, 2); | |
var year = dateSpan.Slice(6); | |
return new DateTime(int.Parse(year), int.Parse(month), int.Parse(day)); | |
} | |
} | |
[MemoryDiagnoser] | |
public class Benchmark3 | |
{ | |
private int[] _myArray; | |
[Params(100, 1000, 10000)] | |
public int Size { get; set; } | |
[GlobalSetup] | |
public void SetUp() | |
{ | |
_myArray = new int[Size]; | |
for (int index = 0; index < Size; index++) | |
{ | |
_myArray[index] = index; | |
} | |
} | |
[Benchmark(Baseline = true)] | |
public int[] GetArray() | |
=> _myArray; | |
[Benchmark] | |
public Span<int> GetSpan() | |
=> _myArray.AsSpan(); | |
} | |
[MemoryDiagnoser] | |
public class BenchmarkStackHeap | |
{ | |
[Params(100, 1000, 10000)] | |
public int Size { get; set; } | |
public void Fill(Span<int> a) | |
{ | |
for (int index = 0; index < Size; index++) | |
{ | |
a[index] = index; | |
} | |
} | |
[Benchmark(Baseline = true)] | |
public void SpanStack() | |
{ | |
Span<int> a = stackalloc int[Size]; | |
Fill(a); | |
} | |
[Benchmark] | |
public void SpanHeap() | |
{ | |
Span<int> a = new int[Size]; | |
Fill(a); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment