Skip to content

Instantly share code, notes, and snippets.

@heathbm
Last active July 5, 2022 06:25
Show Gist options
  • Save heathbm/59783b3d9e08d9556d7a86fa6ee2cd20 to your computer and use it in GitHub Desktop.
Save heathbm/59783b3d9e08d9556d7a86fa6ee2cd20 to your computer and use it in GitHub Desktop.
Enum.TryFormat Benchmarks
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Tests
{
[MemoryDiagnoser]
[BenchmarkCategory(Categories.Libraries)]
public class Perf_Enum
{
public enum Colors
{
Red = 0x1,
Orange = 0x2,
Yellow = 0x4,
Green = 0x8,
Blue = 0x10
}
[Benchmark]
public string FormatG()
{
return Enum.Format(typeof(Colors), Colors.Green, "G");
}
[Benchmark]
public void TryFormatG()
{
Span<char> destination = stackalloc char[5];
Enum.TryFormat(Colors.Green, destination, out _, "G");
}
[Benchmark]
public string FormatD()
{
return Enum.Format(typeof(Colors), Colors.Green, "D");
}
[Benchmark]
public void TryFormatD()
{
Span<char> destination = stackalloc char[5];
Enum.TryFormat(Colors.Green, destination, out _, "D");
}
[Benchmark]
public string FormatX()
{
return Enum.Format(typeof(Colors), Colors.Green, "X");
}
[Benchmark]
public void TryFormatX()
{
Span<char> destination = stackalloc char[5];
Enum.TryFormat(Colors.Green, destination, out _, "X");
}
[Benchmark]
public string FormatF()
{
return Enum.Format(typeof(Colors), Colors.Green, "F");
}
[Benchmark]
public void TryFormatF()
{
Span<char> destination = stackalloc char[5];
Enum.TryFormat(Colors.Green, destination, out _, "F");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment