Created
January 30, 2022 05:23
-
-
Save ufcpp/6326920e1fefe48a91c6f11a05ae9b6e to your computer and use it in GitHub Desktop.
ReadOnlySpan 最適化
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 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