Skip to content

Commit 64b4b66

Browse files
Add LambdaVsSetterBenchmarks
1 parent e7f2341 commit 64b4b66

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

Platform.Setters.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Platform.Setters.Tests", "c
1212
EndProject
1313
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpToCppTranslator", "CSharpToCppTranslator\CSharpToCppTranslator.csproj", "{1394B2AC-0B80-4F29-82C3-A610C3BD6BDB}"
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Platform.Setters.Benchmarks", "csharp\Platform.Setters.Benchmarks\Platform.Setters.Benchmarks.csproj", "{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -30,6 +32,10 @@ Global
3032
{1394B2AC-0B80-4F29-82C3-A610C3BD6BDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
3133
{1394B2AC-0B80-4F29-82C3-A610C3BD6BDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
3234
{1394B2AC-0B80-4F29-82C3-A610C3BD6BDB}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}.Release|Any CPU.ActiveCfg = Release|Any CPU
38+
{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}.Release|Any CPU.Build.0 = Release|Any CPU
3339
EndGlobalSection
3440
GlobalSection(SolutionProperties) = preSolution
3541
HideSolutionNode = FALSE
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using BenchmarkDotNet.Attributes;
6+
namespace Platform.Setters.Benchmarks
7+
{
8+
[SimpleJob]
9+
[MemoryDiagnoser]
10+
public class LambdaVsSetterBenchmarks
11+
{
12+
[GlobalSetup]
13+
public static void Setup()
14+
{
15+
16+
}
17+
18+
[GlobalCleanup]
19+
public static void Cleanup()
20+
{
21+
22+
}
23+
24+
int Process(Func<IList<int>, int> func)
25+
{
26+
return func(new List<int>{0,1,2,3,4,5,6,7,8,9});
27+
}
28+
29+
[Benchmark]
30+
public int LambdaBenchmark()
31+
{
32+
int result;
33+
var trueValue = 1;
34+
var falseValue = 2;
35+
int Lambda(IList<int> list)
36+
{
37+
result = list[0];
38+
return trueValue;
39+
}
40+
return Process(Lambda);
41+
}
42+
43+
[Benchmark]
44+
public int SetterBenchmark()
45+
{
46+
var setter = new Setter<int, int>(1, 2);
47+
return Process(setter.SetFirstAndReturnTrue);
48+
}
49+
50+
}
51+
52+
}
53+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net472;netstandard2.0;netstandard2.1;net5</TargetFrameworks>
6+
<IsPackable>false</IsPackable>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
<LangVersion>latest</LangVersion>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Platform.Setters\Platform.Setters.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using BenchmarkDotNet.Running;
2+
3+
namespace Platform.Setters.Benchmarks
4+
{
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
BenchmarkRunner.Run<LambdaVsSetterBenchmarks>();
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)