Skip to content

Instantly share code, notes, and snippets.

@smoogipoo
Last active October 14, 2021 08:44
Show Gist options
  • Save smoogipoo/badb4174449f4c0245550a4ec17078eb to your computer and use it in GitHub Desktop.
Save smoogipoo/badb4174449f4c0245550a4ec17078eb to your computer and use it in GitHub Desktop.
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
namespace BenchmarksProject
{
[MemoryDiagnoser]
public class LambdaAllocTest
{
[Benchmark]
public void TestInstanceMethodLambdaCapture()
{
for (int i = 0; i < 1000; i++)
method1(() => method2());
}
[Benchmark]
public void TestInstanceMethodDirectCapture()
{
for (int i = 0; i < 1000; i++)
method1(method2);
}
[Benchmark]
public void TestStaticMethodLambdaCapture()
{
for (int i = 0; i < 1000; i++)
method1(() => staticMethod2());
}
[Benchmark]
public void TestStaticMethodDirectCapture()
{
for (int i = 0; i < 1000; i++)
method1(staticMethod2);
}
[Benchmark]
public void TestLocalNonCapturingMethodLambdaCapture()
{
void localMethod2()
{
}
for (int i = 0; i < 1000; i++)
method1(() => localMethod2());
}
[Benchmark]
public void TestLocalNonCapturingMethodDirectCapture()
{
void localMethod2()
{
}
for (int i = 0; i < 1000; i++)
method1(localMethod2);
}
[Benchmark]
public void TestLocalNonCapturingStaticMethodLambdaCapture()
{
static void localMethod2()
{
}
for (int i = 0; i < 1000; i++)
method1(() => localMethod2());
}
[Benchmark]
public void TestLocalNonCapturingStaticMethodDirectCapture()
{
static void localMethod2()
{
}
for (int i = 0; i < 1000; i++)
method1(localMethod2);
}
[Benchmark]
public void TestLocalCapturingMethodLambdaCapture()
{
int x = 0;
void method2() => x++;
for (int i = 0; i < 1000; i++)
method1(() => method2());
}
[Benchmark]
public void TestLocalCapturingMethodDirectCapture()
{
int x = 0;
void method2() => x++;
for (int i = 0; i < 1000; i++)
method1(method2);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void method1(Action action)
{
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void method2()
{
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void staticMethod2()
{
}
}
}
@smoogipoo
Copy link
Author

smoogipoo commented Oct 14, 2021

Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
TestInstanceMethodLambdaCapture 12.098 us 0.1361 us 0.1273 us 0.7629 - - 64000 B
TestInstanceMethodDirectCapture 11.360 us 0.0398 us 0.0352 us 0.7629 - - 64000 B
TestStaticMethodLambdaCapture 2.173 us 0.0013 us 0.0012 us - - - -
TestStaticMethodDirectCapture 12.028 us 0.2275 us 0.2708 us 0.7629 - - 64000 B
TestLocalNonCapturingMethodLambdaCapture 2.179 us 0.0059 us 0.0052 us - - - -
TestLocalNonCapturingMethodDirectCapture 11.646 us 0.1288 us 0.1205 us 0.7629 - - 64000 B
TestLocalNonCapturingStaticMethodLambdaCapture 2.176 us 0.0019 us 0.0015 us - - - -
TestLocalNonCapturingStaticMethodDirectCapture 12.156 us 0.2376 us 0.2222 us 0.7629 - - 64000 B
TestLocalCapturingMethodLambdaCapture 2.201 us 0.0137 us 0.0114 us - - - 96 B
TestLocalCapturingMethodDirectCapture 11.731 us 0.1157 us 0.0966 us 0.7629 - - 64024 B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment