Last active
March 14, 2023 19:51
-
-
Save teoadal/ba5e552c6e5a581ac8545d3485308048 to your computer and use it in GitHub Desktop.
This file contains 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 System.Buffers; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Jobs; | |
namespace Storage.Benchmark; | |
[SimpleJob(RuntimeMoniker.Net60)] | |
[SimpleJob(RuntimeMoniker.NetCoreApp31)] | |
[SimpleJob(RuntimeMoniker.Net48)] | |
[MeanColumn] | |
public class ArrayRandomReadBenchmark | |
{ | |
[Benchmark(Baseline = true)] | |
public int ArrayIndex() | |
{ | |
var sum = 0; | |
foreach (var index in _indices) | |
{ | |
sum += _array[index]; | |
} | |
return sum; | |
} | |
[Benchmark] | |
public int MemoryIndex() | |
{ | |
var sum = 0; | |
foreach (var index in _indices) | |
{ | |
sum += _memory.Span[index]; | |
} | |
return sum; | |
} | |
[Benchmark] | |
public unsafe int Unsafe() | |
{ | |
var sum = 0; | |
fixed (int* arrayPtr = &_array[0]) | |
{ | |
foreach (var index in _indices) | |
{ | |
sum += *(arrayPtr + index); | |
} | |
} | |
return sum; | |
} | |
[Benchmark] | |
public unsafe int UnsafePined() | |
{ | |
var sum = 0; | |
foreach (var index in _indices) | |
{ | |
sum += *((int*)_memoryHandle.Pointer + index); | |
} | |
return sum; | |
} | |
#region Configuration | |
private int[] _array = null!; | |
private ReadOnlyMemory<int> _memory; | |
private ReadOnlyMemory<int> _pinedMemory; | |
private MemoryHandle _memoryHandle; | |
private int[] _indices = null!; | |
[GlobalSetup] | |
public void Init() | |
{ | |
const int count = 1021; | |
_array = new int[count]; | |
_memory = new ReadOnlyMemory<int>(_array); | |
_pinedMemory = new ReadOnlyMemory<int>(_array); | |
_memoryHandle = _pinedMemory.Pin(); | |
var rnd = new Random(1234); | |
for (var i = 0; i < count; i++) | |
{ | |
_array[i] = rnd.Next(0, 10); | |
} | |
_indices = new int[count]; | |
for (var i = 0; i < count; i++) | |
{ | |
_indices[i] = rnd.Next(0, _array.Length - 1); | |
} | |
} | |
[GlobalCleanup] | |
public void Clear() | |
{ | |
_memoryHandle.Dispose(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment