Last active
October 14, 2021 08:44
-
-
Save smoogipoo/badb4174449f4c0245550a4ec17078eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.