Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created July 21, 2018 04:30
Show Gist options
  • Save danielplawgo/45d10ea21c7b162d10c4b91310159a66 to your computer and use it in GitHub Desktop.
Save danielplawgo/45d10ea21c7b162d10c4b91310159a66 to your computer and use it in GitHub Desktop.
Jak zmierzyć wydajność kodu .NET? BenchmarkDotNet
public static string ToDisplayString(this Enum value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string description = value.ToString();
EnumDescriptionAttribute[] attributes = (EnumDescriptionAttribute[])value.GetType().GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
Type resourceType = attributes[0].ResourceType;
if (resourceType != null)
{
var property = resourceType.GetProperty(description);
if (property != null)
{
var propertyValue = property.GetValue(null);
if (propertyValue != null)
{
description = propertyValue.ToString();
}
}
}
}
return description;
}
public static class EnumExtensions
{
public static string ToDisplayStringResources(this OrderStatus value)
{
switch (value)
{
case OrderStatus.Created:
return OrderStatusResources.Created;
case OrderStatus.Completed:
return OrderStatusResources.Completed;
case OrderStatus.Canceled:
return OrderStatusResources.Canceled;
default:
throw new ArgumentOutOfRangeException(nameof(value), value, null);
}
}
public static string ToDisplayStringReflection(this Enum value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string description = value.ToString();
EnumDescriptionAttribute[] attributes = (EnumDescriptionAttribute[])value.GetType().GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
Type resourceType = attributes[0].ResourceType;
if (resourceType != null)
{
var property = resourceType.GetProperty(description);
if (property != null)
{
var propertyValue = property.GetValue(null);
if (propertyValue != null)
{
description = propertyValue.ToString();
}
}
}
}
return description;
}
}
private static IDictionary<Type, ResourceManager> _resourceManagers = new ConcurrentDictionary<Type, ResourceManager>();
public static string ToDisplayStringUsingResourceManagerAndCache(this Enum value, CultureInfo culture = null)
{
string description = value.ToString();
ResourceManager resourceManager = null;
if (_resourceManagers.TryGetValue(value.GetType(), out resourceManager) == false)
{
EnumDescriptionAttribute[] attributes = (EnumDescriptionAttribute[])value.GetType().GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
if (attributes.Any())
{
Type resourceType = attributes[0].ResourceType;
if (resourceType != null)
{
var resourceManagerProperty = resourceType.GetProperty("ResourceManager");
if (resourceManagerProperty != null)
{
resourceManager = resourceManagerProperty.GetValue(null) as ResourceManager;
_resourceManagers.Add(value.GetType(), resourceManager);
}
}
}
}
if (resourceManager != null)
{
return resourceManager.GetString(description, culture ?? Thread.CurrentThread.CurrentUICulture);
}
return description;
}
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<ToDisplayStringBenchmarks>();
}
}
BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
Intel Core i7-4790K CPU 4.00GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
Frequency=3897226 Hz, Resolution=256.5928 ns, Timer=TSC
[Host] : .NET Framework 4.6 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.3131.0
DefaultJob : .NET Framework 4.6 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.3131.0
```
| Method | Mean | Error | StdDev |
|----------- |------------:|-----------:|-----------:|
| Resources | 99.83 ns | 0.6599 ns | 0.5850 ns |
| Reflection | 4,572.03 ns | 30.9951 ns | 28.9928 ns |
BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
Intel Core i7-4790K CPU 4.00GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
Frequency=3897226 Hz, Resolution=256.5928 ns, Timer=TSC
[Host] : .NET Framework 4.6 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.3131.0
DefaultJob : .NET Framework 4.6 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.3131.0
Method | Mean | Error | StdDev |
----------------------------- |-----------:|----------:|----------:|
Resources | 100.7 ns | 1.211 ns | 1.133 ns |
Reflection | 4,609.6 ns | 54.148 ns | 50.650 ns |
UsingResourceManagerAndCache | 755.6 ns | 6.128 ns | 5.732 ns |
Method | Mean | Error | StdDev |
----------------------------- |-----------:|-----------:|-----------:|
Resources | 100.6 ns | 0.9496 ns | 0.8883 ns |
Reflection | 4,599.0 ns | 16.3676 ns | 13.6677 ns |
UsingResourceManagerAndCache | 755.7 ns | 2.6123 ns | 2.3158 ns |
EnumToString | 604.1 ns | 1.4914 ns | 1.2454 ns |
public class ToDisplayStringBenchmarks
{
protected OrderStatus OrderStatus { get; set; }
public ToDisplayStringBenchmarks()
{
OrderStatus = OrderStatus.Completed;
}
[Benchmark]
public string Resources()
{
return OrderStatus.ToDisplayStringResources();
}
[Benchmark]
public string Reflection()
{
return OrderStatus.ToDisplayStringReflection();
}
}
[Benchmark]
public string UsingResourceManagerAndCache()
{
return OrderStatus.ToDisplayStringUsingResourceManagerAndCache();
}
[Benchmark]
public string EnumToString()
{
return OrderStatus.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment