From 64b4b66ba7ede4617c1f0171f4a3e434286890aa Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Mon, 10 Jan 2022 18:33:25 +0600 Subject: [PATCH 1/2] Add `LambdaVsSetterBenchmarks` --- Platform.Setters.sln | 6 +++ .../LambdaVsSetterBenchmarks.cs | 53 +++++++++++++++++++ .../Platform.Setters.Benchmarks.csproj | 19 +++++++ csharp/Platform.Setters.Benchmarks/Program.cs | 12 +++++ 4 files changed, 90 insertions(+) create mode 100644 csharp/Platform.Setters.Benchmarks/LambdaVsSetterBenchmarks.cs create mode 100644 csharp/Platform.Setters.Benchmarks/Platform.Setters.Benchmarks.csproj create mode 100644 csharp/Platform.Setters.Benchmarks/Program.cs diff --git a/Platform.Setters.sln b/Platform.Setters.sln index cbe0cb5..1d345ab 100644 --- a/Platform.Setters.sln +++ b/Platform.Setters.sln @@ -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 @@ -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 diff --git a/csharp/Platform.Setters.Benchmarks/LambdaVsSetterBenchmarks.cs b/csharp/Platform.Setters.Benchmarks/LambdaVsSetterBenchmarks.cs new file mode 100644 index 0000000..de6cc9e --- /dev/null +++ b/csharp/Platform.Setters.Benchmarks/LambdaVsSetterBenchmarks.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using BenchmarkDotNet.Attributes; +namespace Platform.Setters.Benchmarks +{ + [SimpleJob] + [MemoryDiagnoser] + public class LambdaVsSetterBenchmarks + { + [GlobalSetup] + public static void Setup() + { + + } + + [GlobalCleanup] + public static void Cleanup() + { + + } + + int Process(Func, int> func) + { + return func(new List{0,1,2,3,4,5,6,7,8,9}); + } + + [Benchmark] + public int LambdaBenchmark() + { + int result; + var trueValue = 1; + var falseValue = 2; + int Lambda(IList list) + { + result = list[0]; + return trueValue; + } + return Process(Lambda); + } + + [Benchmark] + public int SetterBenchmark() + { + var setter = new Setter(1, 2); + return Process(setter.SetFirstAndReturnTrue); + } + + } + +} + diff --git a/csharp/Platform.Setters.Benchmarks/Platform.Setters.Benchmarks.csproj b/csharp/Platform.Setters.Benchmarks/Platform.Setters.Benchmarks.csproj new file mode 100644 index 0000000..0415083 --- /dev/null +++ b/csharp/Platform.Setters.Benchmarks/Platform.Setters.Benchmarks.csproj @@ -0,0 +1,19 @@ + + + + Exe + net472;netstandard2.0;netstandard2.1;net5 + false + true + latest + + + + + + + + + + + diff --git a/csharp/Platform.Setters.Benchmarks/Program.cs b/csharp/Platform.Setters.Benchmarks/Program.cs new file mode 100644 index 0000000..d7b3694 --- /dev/null +++ b/csharp/Platform.Setters.Benchmarks/Program.cs @@ -0,0 +1,12 @@ +using BenchmarkDotNet.Running; + +namespace Platform.Setters.Benchmarks +{ + class Program + { + static void Main() + { + BenchmarkRunner.Run(); + } + } +} From 07840506e8f53c95473c507762283c5c5486d3d8 Mon Sep 17 00:00:00 2001 From: FreePhoenix888 Date: Tue, 11 Jan 2022 09:09:03 +0600 Subject: [PATCH 2/2] `Cleanup` --- .../LambdaVsSetterBenchmarks.cs | 66 +++++++------------ 1 file changed, 23 insertions(+), 43 deletions(-) diff --git a/csharp/Platform.Setters.Benchmarks/LambdaVsSetterBenchmarks.cs b/csharp/Platform.Setters.Benchmarks/LambdaVsSetterBenchmarks.cs index de6cc9e..2eccce8 100644 --- a/csharp/Platform.Setters.Benchmarks/LambdaVsSetterBenchmarks.cs +++ b/csharp/Platform.Setters.Benchmarks/LambdaVsSetterBenchmarks.cs @@ -1,53 +1,33 @@ +using BenchmarkDotNet.Attributes; using System; -using System.Collections; using System.Collections.Generic; -using System.Diagnostics; -using BenchmarkDotNet.Attributes; -namespace Platform.Setters.Benchmarks -{ - [SimpleJob] - [MemoryDiagnoser] - public class LambdaVsSetterBenchmarks - { - [GlobalSetup] - public static void Setup() - { - } +namespace Platform.Setters.Benchmarks; - [GlobalCleanup] - public static void Cleanup() - { - - } - - int Process(Func, int> func) - { - return func(new List{0,1,2,3,4,5,6,7,8,9}); - } - - [Benchmark] - public int LambdaBenchmark() - { - int result; - var trueValue = 1; - var falseValue = 2; - int Lambda(IList list) - { - result = list[0]; - return trueValue; - } - return Process(Lambda); - } +[SimpleJob] +[MemoryDiagnoser] +public class LambdaVsSetterBenchmarks +{ + private int Process(Func, int> func) => func(new List { 0, 1, 2, 3 }); - [Benchmark] - public int SetterBenchmark() + [Benchmark] + public int LambdaBenchmark() + { + int result; + var trueValue = 1; + var falseValue = 2; + int Lambda(IList list) { - var setter = new Setter(1, 2); - return Process(setter.SetFirstAndReturnTrue); + result = list[0]; + return trueValue; } - + return Process(Lambda); } + [Benchmark] + public int SetterBenchmark() + { + var setter = new Setter(1, 2); + return Process(setter.SetFirstAndReturnTrue); + } } -