Skip to content

Instantly share code, notes, and snippets.

@Tornhoof
Created April 6, 2018 14:57
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 Tornhoof/9784f7a957b518702f0d3866a47a75b6 to your computer and use it in GitHub Desktop.
Save Tornhoof/9784f7a957b518702f0d3866a47a75b6 to your computer and use it in GitHub Desktop.
Utf8Formatter vs CoreCLR
using System;
using System.Buffers.Text;
using System.Globalization;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace FormatterBenchmark
{
internal class Program
{
private static void Main(string[] args)
{
BenchmarkRunner.Run<Benchmark>();
}
}
[MemoryDiagnoser]
public class Benchmark
{
private static readonly DateTime DateTimeValue = DateTime.Now;
private static readonly char[] DateTimeFormat = {'o'};
private static readonly long Int64Value = 123456789012L;
private static readonly double DoubleValue = 12345.67890d;
private static readonly decimal DecimalValue = 12345.67890m;
private static readonly Guid GuidValue = Guid.NewGuid();
[Benchmark(Description = "DateTime CoreCLR")]
public string CoreClrDateTime()
{
Span<char> span = stackalloc char[40];
int pos = 0;
DateTimeValue.TryFormat(span, out pos, DateTimeFormat.AsSpan(), CultureInfo.InvariantCulture);
return span.Slice(0, pos).ToString();
}
[Benchmark(Description = "DateTime Utf8Formatter")]
public byte[] Utf8FormatterDateTime()
{
var result = new byte[40];
Span<byte> span = result;
Utf8Formatter.TryFormat(DateTimeValue, span, out var written);
return result;
}
[Benchmark(Description = "Int64 CoreCLR")]
public string CoreClrLong()
{
Span<char> span = stackalloc char[40];
int pos = 0;
Int64Value.TryFormat(span, out pos, null, CultureInfo.InvariantCulture);
return span.Slice(0, pos).ToString();
}
[Benchmark(Description = "Int64 Utf8Formatter")]
public byte[] Utf8FormatterLong()
{
var result = new byte[40];
Span<byte> span = result;
Utf8Formatter.TryFormat(Int64Value, span, out var written);
return result;
}
[Benchmark(Description = "Double CoreCLR")]
public string CoreClrDouble()
{
Span<char> span = stackalloc char[40];
int pos = 0;
DoubleValue.TryFormat(span, out pos, null, CultureInfo.InvariantCulture);
return span.Slice(0, pos).ToString();
}
[Benchmark(Description = "Double Utf8Formatter")]
public byte[] Utf8FormatterDouble()
{
var result = new byte[40];
Span<byte> span = result;
Utf8Formatter.TryFormat(DoubleValue, span, out var written);
return result;
}
[Benchmark(Description = "Decimal CoreCLR")]
public string CoreClrDecimal()
{
Span<char> span = stackalloc char[40];
int pos = 0;
DecimalValue.TryFormat(span, out pos, null, CultureInfo.InvariantCulture);
return span.Slice(0, pos).ToString();
}
[Benchmark(Description = "Decimal Utf8Formatter")]
public byte[] Utf8FormatterDecimal()
{
var result = new byte[40];
Span<byte> span = result;
Utf8Formatter.TryFormat(DecimalValue, span, out var written);
return result;
}
[Benchmark(Description = "Guid CoreCLR")]
public string CoreClrGuid()
{
Span<char> span = stackalloc char[40];
int pos = 0;
GuidValue.TryFormat(span, out pos, null);
return span.Slice(0, pos).ToString();
}
[Benchmark(Description = "Guid Utf8Formatter")]
public byte[] Utf8FormatterGuid()
{
var result = new byte[40];
Span<byte> span = result;
Utf8Formatter.TryFormat(GuidValue, span, out var written);
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment