Skip to content

Instantly share code, notes, and snippets.

@sonnemaf
Created February 18, 2020 09:03
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 sonnemaf/75871a74c952c1c50bc132b877b9d4fa to your computer and use it in GitHub Desktop.
Save sonnemaf/75871a74c952c1c50bc132b877b9d4fa to your computer and use it in GitHub Desktop.
Source used in a tweet
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Linq;
public class Program {
static void Main(string[] args) => BenchmarkRunner.Run(typeof(Program));
private Func<int, int> _delegateToStaticMethod;
private Func<int, int> _delegateToInstanceMethod;
private static int StaticTarget(int x) => x + 1;
private int InstanceTarget(int x) => x + 1;
[GlobalSetup]
public void Setup() {
_delegateToStaticMethod = StaticTarget;
_delegateToInstanceMethod = InstanceTarget;
}
[Benchmark]
public int DelegateToInstanceMethodInt() =>_delegateToInstanceMethod(1);
[Benchmark]
public int DelegateToStaticMethodInt() => _delegateToStaticMethod(1);
[Benchmark]
public void DelegateToInstanceMethodVoid() => _delegateToInstanceMethod(1);
[Benchmark]
public void DelegateToStaticMethodVoid() => _delegateToStaticMethod(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment