Created
April 17, 2019 02:22
-
-
Save ufcpp/db95a881f079ee3e694545c57673883f to your computer and use it in GitHub Desktop.
もしやと思ってやってみたら Binary の方がほんのちょっと(1% くらい)速いの受ける
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; | |
using System; | |
using System.Runtime.InteropServices; | |
public class DecimalConstantBenchmark | |
{ | |
const int N = 10000; | |
const decimal ConstDecimal = 123456789.123456789m; | |
static ReadOnlySpan<byte> bin => new byte[] { 0, 0, 9, 0, 0, 0, 0, 0, 21, 95, 208, 172, 75, 155, 182, 1 }; | |
static decimal ReadFromBinary => MemoryMarshal.Cast<byte, decimal>(bin)[0]; | |
[Benchmark] | |
public decimal Const() | |
{ | |
var sum = 0m; | |
for (int i = 0; i < N; i++) sum += ConstDecimal; | |
return sum; | |
} | |
[Benchmark] | |
public decimal Binary() | |
{ | |
var sum = 0m; | |
for (int i = 0; i < N; i++) sum += ReadFromBinary; | |
return sum; | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
BenchmarkRunner.Run<DecimalConstantBenchmark>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment