Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created January 30, 2022 05:23
Show Gist options
  • Save ufcpp/6326920e1fefe48a91c6f11a05ae9b6e to your computer and use it in GitHub Desktop.
Save ufcpp/6326920e1fefe48a91c6f11a05ae9b6e to your computer and use it in GitHub Desktop.
ReadOnlySpan 最適化
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<SpanBenchmark>();
[MemoryDiagnoser]
public class SpanBenchmark
{
private const int N = 1000;
[Benchmark]
public void IntArray()
{
for (int i = 0; i < N; i++)
{
m(i);
}
static int m(int i)
{
ReadOnlySpan<int> table = new[] { 1, 0, -1, 0 }; // 差はこの行だけ
return table[i % 4];
}
}
[Benchmark]
public void SByteArray()
{
for (int i = 0; i < N; i++)
{
m(i);
}
static int m(int i)
{
ReadOnlySpan<sbyte> table = new sbyte[] { 1, 0, -1, 0 }; // 差はこの行だけ
return table[i % 4];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment