Skip to content

Instantly share code, notes, and snippets.

@svick
Last active May 25, 2020 20:58
Show Gist options
  • Save svick/d2bd0cffb6f14fb1a2f1e1978d8ff883 to your computer and use it in GitHub Desktop.
Save svick/d2bd0cffb6f14fb1a2f1e1978d8ff883 to your computer and use it in GitHub Desktop.
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
public class Program
{
static void Main() => BenchmarkRunner.Run<Benchmark>();
}
[MemoryDiagnoser]
public unsafe class Benchmark
{
private Guid guid = Guid.Empty;
[Benchmark(Baseline = true)]
public string FastAllocateString()
{
const int length = 32;
var uuidString = Dodo.Primitives.IL.CoreLib.FastAllocateString(length);
fixed (char* uuidChars = uuidString)
{
FormatN(new Span<char>(uuidChars, length));
}
return uuidString;
}
[Benchmark]
public string StringCreate()
{
return string.Create(32, this, (span, self) => self.FormatN(span));
}
private void FormatN(Span<char> span) => guid.TryFormat(span, out _, "N");
}
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18362.836 (1903/May2019Update/19H1)
Intel Core i5-2300 CPU 2.80GHz (Sandy Bridge), 1 CPU, 4 logical and 4 physical cores
.NET Core SDK=5.0.100-preview.4.20258.7
  [Host]     : .NET Core 5.0.0 (CoreCLR 5.0.20.25106, CoreFX 5.0.20.25106), X64 RyuJIT
  DefaultJob : .NET Core 5.0.0 (CoreCLR 5.0.20.25106, CoreFX 5.0.20.25106), X64 RyuJIT
Method Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
FastAllocateString 77.35 ns 0.460 ns 0.430 ns 1.00 0.0280 - - 88 B
StringCreate 85.58 ns 0.503 ns 0.470 ns 1.11 0.0280 - - 88 B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment