Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created April 17, 2019 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ufcpp/db95a881f079ee3e694545c57673883f to your computer and use it in GitHub Desktop.
Save ufcpp/db95a881f079ee3e694545c57673883f to your computer and use it in GitHub Desktop.
もしやと思ってやってみたら Binary の方がほんのちょっと(1% くらい)速いの受ける
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