Skip to content

Instantly share code, notes, and snippets.

@kshyju
Created August 12, 2022 23:26
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 kshyju/d4318e0b2c1542cb327c0a4090a80f46 to your computer and use it in GitHub Desktop.
Save kshyju/d4318e0b2c1542cb327c0a4090a80f46 to your computer and use it in GitHub Desktop.
Enum benchmarks tp get enum value string
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
11th Gen Intel Core i7-1185G7 3.00GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK=7.0.100-preview.7.22377.5
  [Host]     : .NET 6.0.8 (6.0.822.36306), X64 RyuJIT
  DefaultJob : .NET 6.0.8 (6.0.822.36306), X64 RyuJIT

Method Mean Error StdDev Gen 0 Allocated
CastValueAndToString 19.77 ns 0.495 ns 0.643 ns 0.0038 24 B
EnumGetType 61.26 ns 1.321 ns 1.622 ns 0.0038 24 B
using BenchmarkDotNet.Attributes;
namespace Benchmarks
{
public enum AuthorizationLevel
{
Anonymous,
User,
Function,
System,
Admin
}
[MemoryDiagnoser]
public class EnumBenchmarks
{
[Benchmark]
public string CastValueAndToString() => ((AuthorizationLevel)2).ToString();
[Benchmark]
public string EnumGetType() => Enum.GetName(typeof(AuthorizationLevel), 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment