Skip to content

Commit df3439c

Browse files
Merge pull request #43 from linksplatform/feature/benchmarks/add_lambda_vs_setter_benchmarks
Add `LambdaVsSetterBenchmarks`
2 parents 101ec79 + 0784050 commit df3439c

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using BenchmarkDotNet.Attributes;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Platform.Setters.Benchmarks;
6+
7+
[SimpleJob]
8+
[MemoryDiagnoser]
9+
public class LambdaVsSetterBenchmarks
10+
{
11+
private int Process(Func<IList<int>, int> func) => func(new List<int> { 0, 1, 2, 3 });
12+
13+
[Benchmark]
14+
public int LambdaBenchmark()
15+
{
16+
int result;
17+
var trueValue = 1;
18+
var falseValue = 2;
19+
int Lambda(IList<int> list)
20+
{
21+
result = list[0];
22+
return trueValue;
23+
}
24+
return Process(Lambda);
25+
}
26+
27+
[Benchmark]
28+
public int SetterBenchmark()
29+
{
30+
var setter = new Setter<int, int>(1, 2);
31+
return Process(setter.SetFirstAndReturnTrue);
32+
}
33+
}
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)