Created
July 21, 2018 04:30
-
-
Save danielplawgo/45d10ea21c7b162d10c4b91310159a66 to your computer and use it in GitHub Desktop.
Jak zmierzyć wydajność kodu .NET? BenchmarkDotNet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
BenchmarkRunner.Run<ToDisplayStringBenchmarks>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Benchmark] | |
public string UsingResourceManagerAndCache() | |
{ | |
return OrderStatus.ToDisplayStringUsingResourceManagerAndCache(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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