.NET Framework 4.8 v .NET 7 Reflection Benchmarks
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 BenchmarkDotNet.Running; | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
//choose on command line from multiple benchmarks | |
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(); | |
} | |
} |
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFrameworks>net48;net6.0;net7.0;net8.0</TargetFrameworks> | |
<ImplicitUsings>disable</ImplicitUsings> | |
<LangVersion>latest</LangVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="BenchmarkDotNet" Version="0.13.3.2064" /> | |
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" /> | |
</ItemGroup> | |
</Project> | |
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.Linq; | |
using System.Reflection; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Columns; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Jobs; | |
using BenchmarkDotNet.Reports; | |
namespace BenchmarkDotNet.Samples | |
{ | |
[MemoryDiagnoser] | |
[Config(typeof(Config))] | |
[SimpleJob(RuntimeMoniker.Net48, baseline: true)] | |
[SimpleJob(RuntimeMoniker.Net70)] | |
[HideColumns(Column.Job, Column.RatioSD, Column.AllocRatio)] | |
public class ReflectionBenchmarks | |
{ | |
[Benchmark] | |
public void MethodInvoke() | |
{ | |
var instance = new Target(); | |
var method = | |
typeof(Target) | |
.GetMethod("One", | |
BindingFlags.Instance | | |
BindingFlags.Public | |
); | |
method.Invoke(instance, parameters: null); | |
} | |
[Benchmark] | |
public void PrivateMethodInvoke() | |
{ | |
var instance = new Target(); | |
var method = | |
typeof(Target) | |
.GetMethod("GetSecret", | |
BindingFlags.Instance | | |
BindingFlags.NonPublic | |
); | |
var result = | |
method.Invoke(instance, parameters: null); | |
} | |
[Benchmark] | |
public void PrivateField() | |
{ | |
var instance = new Target(); | |
var field = | |
typeof(Target) | |
.GetField("secret", | |
BindingFlags.Instance | | |
BindingFlags.NonPublic | |
); | |
var value = | |
(string)field.GetValue(instance); | |
} | |
[Benchmark] | |
public void GenericMethod() | |
{ | |
var instance = new Target(); | |
var method = | |
typeof(Target) | |
.GetMethod("Generic", | |
BindingFlags.Instance | | |
BindingFlags.Public | |
); | |
var generic = | |
method.MakeGenericMethod(typeof(string)); | |
var result = | |
generic.Invoke(instance, new[] { "David" }); | |
} | |
[Benchmark] | |
public void AllPublicMethods() | |
{ | |
var instance = new Target(); | |
var members = | |
typeof(Target) | |
.GetMembers( | |
BindingFlags.Instance | | |
BindingFlags.Public | | |
BindingFlags.DeclaredOnly | |
) | |
.Where(x => | |
!x.Name.StartsWith("get_") && | |
!x.Name.StartsWith("set_") | |
) | |
.ToList(); | |
} | |
public class Target | |
{ | |
public void One(){ | |
var one = 1; | |
} | |
private string GetSecret() => "42"; | |
private string secret = "42"; | |
public string Generic<T>(T input) => $"Hello {input}!"; | |
public void Zero() { } | |
public string OneA { get; set; } | |
public string TwoB { get; set; } | |
} | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
SummaryStyle = | |
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment