Skip to content

Commit

Permalink
Merge pull request #43 from linksplatform/feature/benchmarks/add_lamb…
Browse files Browse the repository at this point in the history
…da_vs_setter_benchmarks

Add `LambdaVsSetterBenchmarks`
  • Loading branch information
FreePhoenix888 authored Jan 11, 2022
2 parents 101ec79 + 0784050 commit df3439c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Platform.Setters.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Platform.Setters.Tests", "c
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpToCppTranslator", "CSharpToCppTranslator\CSharpToCppTranslator.csproj", "{1394B2AC-0B80-4F29-82C3-A610C3BD6BDB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Platform.Setters.Benchmarks", "csharp\Platform.Setters.Benchmarks\Platform.Setters.Benchmarks.csproj", "{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -30,6 +32,10 @@ Global
{1394B2AC-0B80-4F29-82C3-A610C3BD6BDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1394B2AC-0B80-4F29-82C3-A610C3BD6BDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1394B2AC-0B80-4F29-82C3-A610C3BD6BDB}.Release|Any CPU.Build.0 = Release|Any CPU
{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B7CF42F-46F9-480E-B50D-1AA9A9F02E29}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
33 changes: 33 additions & 0 deletions csharp/Platform.Setters.Benchmarks/LambdaVsSetterBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;

namespace Platform.Setters.Benchmarks;

[SimpleJob]
[MemoryDiagnoser]
public class LambdaVsSetterBenchmarks
{
private int Process(Func<IList<int>, int> func) => func(new List<int> { 0, 1, 2, 3 });

[Benchmark]
public int LambdaBenchmark()
{
int result;
var trueValue = 1;
var falseValue = 2;
int Lambda(IList<int> list)
{
result = list[0];
return trueValue;
}
return Process(Lambda);
}

[Benchmark]
public int SetterBenchmark()
{
var setter = new Setter<int, int>(1, 2);
return Process(setter.SetFirstAndReturnTrue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net472;netstandard2.0;netstandard2.1;net5</TargetFrameworks>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Platform.Setters\Platform.Setters.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions csharp/Platform.Setters.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BenchmarkDotNet.Running;

namespace Platform.Setters.Benchmarks
{
class Program
{
static void Main()
{
BenchmarkRunner.Run<LambdaVsSetterBenchmarks>();
}
}
}

0 comments on commit df3439c

Please sign in to comment.