Skip to content

Instantly share code, notes, and snippets.

@svick
Last active February 22, 2018 15:37
Show Gist options
  • Select an option

  • Save svick/1dd12c4eb054b568bc1c1652ef452159 to your computer and use it in GitHub Desktop.

Select an option

Save svick/1dd12c4eb054b568bc1c1652ef452159 to your computer and use it in GitHub Desktop.
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
[Config(typeof(Config))]
public class Program
{
public class Config : ManualConfig
{
public Config() => Add(MemoryDiagnoser.Default);
}
static void Main() => BenchmarkRunner.Run<Program>();
static Func<int[]> Lambda(int[] xs)
{
return () => xs;
}
static Func<int[]> ExtensionMethod(int[] xs)
{
return xs.Identity;
}
private static int[] array = new int[0];
[Benchmark]
public void CreateDelegateLambda() => Lambda(array);
[Benchmark]
public void CreateDelegateExtensionMethod() => ExtensionMethod(array);
[Benchmark]
public void InvokeDelegateLambda() => Lambda(array).Invoke();
[Benchmark]
public void InvokeDelegateExtensionMethod() => ExtensionMethod(array).Invoke();
}
static class Extensions
{
public static int[] Identity(this int[] xs) => xs;
}
BenchmarkDotNet=v0.10.11, OS=Windows 10 Redstone 3 [1709, Fall Creators Update] (10.0.16299.248)
Processor=Intel Core i5-2300 CPU 2.80GHz (Sandy Bridge), ProcessorCount=4
Frequency=2727543 Hz, Resolution=366.6303 ns, Timer=TSC
.NET Core SDK=2.1.300-preview2-008042
[Host] : .NET Core 2.0.5 (Framework 4.6.26020.03), 64bit RyuJIT
DefaultJob : .NET Core 2.0.5 (Framework 4.6.26020.03), 64bit RyuJIT
Method | Mean | Error | StdDev | Gen 0 | Allocated |
------------------------------ |---------:|----------:|----------:|-------:|----------:|
CreateDelegateLambda | 36.74 ns | 0.7785 ns | 0.9268 ns | 0.0280 | 88 B |
CreateDelegateExtensionMethod | 18.38 ns | 0.5607 ns | 1.6532 ns | 0.0203 | 64 B |
InvokeDelegateLambda | 37.26 ns | 0.9130 ns | 2.6776 ns | 0.0280 | 88 B |
InvokeDelegateExtensionMethod | 23.83 ns | 0.6927 ns | 2.0424 ns | 0.0203 | 64 B |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment