experiments/csharp/E064-DynamicVsReflectionBenchmarks/Program.cs

30 lines
587 B
C#
Raw Normal View History

2024-05-04 20:16:52 +00:00
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<DynamicVsReflectionBenchmarks>();
[SimpleJob, MemoryDiagnoser(false)]
public class DynamicVsReflectionBenchmarks
{
[Benchmark]
public int Normal()
{
var s = "Hello World";
return s.Length;
}
[Benchmark]
public int Dynamic()
{
dynamic s = "Hello World";
return s.Length;
}
[Benchmark]
public int Reflection()
{
var s = "Hello World";
return (int)typeof(string).GetProperty("Length").GetValue(s);
}
}