前回メソッドの引数は object 型 > int 型 > ref int 型 で左に行くほど遅いと予想したので、雑に書いてみました。
以前の記事
rksoftware.hatenablog.com rksoftware.hatenablog.com
検証コード
namespace ClassLibrary1; public static class MethodExtensions { public static int ExtensionMethod(this object s) => 0; public static int ExtensionMethodInt(this int s) => 0; public static int ExtensionMethodRefInt(this ref int s) => 0; }
using ClassLibrary1; BenchmarkDotNet.Running.BenchmarkRunner.Run<Sample>(new BenchmarkDotNet.Configs.DebugInProcessConfig()); public class Sample { int i = 0; [BenchmarkDotNet.Attributes.Benchmark] public void MethodObject() =>i.ExtensionMethod(); [BenchmarkDotNet.Attributes.Benchmark] public void MethodInt() => i.ExtensionMethodInt(); [BenchmarkDotNet.Attributes.Benchmark] public void MethodRefInt() => i.ExtensionMethodRefInt(); }
実行
Method | Mean | Error | StdDev | Median |
---|---|---|---|---|
MethodObject | 2.3558 ns | 0.0380 ns | 0.0507 ns | 2.3361 ns |
MethodInt | 0.0631 ns | 0.0379 ns | 0.0654 ns | 0.0339 ns |
MethodRefInt | 0.0153 ns | 0.0215 ns | 0.0353 ns | 0.0000 ns |
はい
はい