Created
April 18, 2020 09:46
-
-
Save hazzik/00c559a774ccc2d72de0b2791bc6595d to your computer and use it in GitHub Desktop.
This file contains 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
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