Skip to content

Instantly share code, notes, and snippets.

@sunghwan2789
Last active September 30, 2022 04:58
Show Gist options
  • Save sunghwan2789/98d831b6459b271255788200e294cd6e to your computer and use it in GitHub Desktop.
Save sunghwan2789/98d831b6459b271255788200e294cd6e to your computer and use it in GitHub Desktop.
BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22622.601)
AMD Ryzen 7 5800H with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK=6.0.401
  [Host]     : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2
  DefaultJob : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2

Method Mean Error StdDev
RegualarProperty 0.3041 ns 0.0348 ns 0.0325 ns
Reflection 104.3792 ns 2.0255 ns 1.8946 ns
CachedReflection 84.2200 ns 1.2290 ns 1.1496 ns
Expression 39,636.0286 ns 222.8135 ns 197.5183 ns
CachedExpression 1.2319 ns 0.0605 ns 0.0566 ns
ExpressionDelegate 42,666.0209 ns 514.6186 ns 481.3745 ns
CachedExpressionDelegate 304.5184 ns 0.9312 ns 0.7776 ns
using System.Linq.Expressions;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<ReflectionPerformance>();
public class ReflectionPerformance
{
private static Person p = new() { Name = "bob" };
[Benchmark]
public string RegualarProperty()
{
return $"{p.Name}";
}
[Benchmark]
public string Reflection()
{
var property = Person.GetPropertyInfo();
return $"{property.GetValue(p)}";
}
private static readonly PropertyInfo cachedProperty = Person.GetPropertyInfo();
[Benchmark]
public string CachedReflection()
{
return $"{cachedProperty.GetValue(p)}";
}
[Benchmark]
public string Expression()
{
var propertyGetter = Person.CreatePropertyGetterExpression();
return $"{propertyGetter(p)}";
}
private static readonly Func<Person, string> cachedPropertyGetter = Person.CreatePropertyGetterExpression();
[Benchmark]
public string CachedExpression()
{
return $"{cachedPropertyGetter(p)}";
}
[Benchmark]
public string ExpressionDelegate()
{
var propertyGetter = Person.CreatePropertyGetterExpressionDelegate();
return $"{propertyGetter.DynamicInvoke(p)}";
}
private static readonly Delegate cachedDelegate = Person.CreatePropertyGetterExpressionDelegate();
[Benchmark]
public string CachedExpressionDelegate()
{
return $"{cachedDelegate.DynamicInvoke(p)}";
}
}
public class Person
{
public string Name { get; set; }
public static PropertyInfo GetPropertyInfo()
{
return typeof(Person).GetProperty(nameof(Name));
}
public static Func<Person, string> CreatePropertyGetterExpression()
{
ParameterExpression arg = Expression.Parameter(typeof(Person), "x");
Expression expr = Expression.Property(arg, nameof(Name));
var propertyGetter = Expression.Lambda<Func<Person, string>>(expr, arg).Compile();
return propertyGetter;
}
public static Delegate CreatePropertyGetterExpressionDelegate()
{
ParameterExpression arg = Expression.Parameter(typeof(Person), "x");
Expression expr = Expression.Property(arg, nameof(Name));
var propertyGetter = Expression.Lambda(expr, arg).Compile();
return propertyGetter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment