Skip to content

Instantly share code, notes, and snippets.

@hazzik
Created April 18, 2020 09:46
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 hazzik/00c559a774ccc2d72de0b2791bc6595d to your computer and use it in GitHub Desktop.
Save hazzik/00c559a774ccc2d72de0b2791bc6595d to your computer and use it in GitHub Desktop.
using System;
using System.Linq.Expressions;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace FastGetMethodBenchmark
{
public class FastGetMethodVsGetMethod
{
[Benchmark]
public MethodInfo FastGetMethod()
{
return FastGetMethod(A, 0);
}
public static MethodInfo FastGetMethod<T, TResult>(Func<T, TResult> func, T a)
{
return func.Method;
}
[Benchmark(Baseline = true)]
public MethodInfo GetMethod()
{
return GetMethod(() => B(0));
}
public static MethodInfo GetMethod(Expression<Action> method)
{
if (method == null)
throw new ArgumentNullException(nameof(method));
return ((MethodCallExpression)method.Body).Method;
}
public int A(int b)=>b;
public int B(int b)=>b;
}
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<FastGetMethodVsGetMethod>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment