From 1d35f2889cf9ffe79cc22340b5ee813c7f84e785 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 4 May 2024 12:37:44 +0100 Subject: [PATCH] Add lambda benchmarks. --- AaruBenchmark/Benchs.cs | 44 ++++++++++++++++++++++++++++++++++++++++ AaruBenchmark/Program.cs | 1 + 2 files changed, 45 insertions(+) diff --git a/AaruBenchmark/Benchs.cs b/AaruBenchmark/Benchs.cs index 723ad90..66cc3c5 100644 --- a/AaruBenchmark/Benchs.cs +++ b/AaruBenchmark/Benchs.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Configs; using BenchmarkDotNet.Environments; @@ -802,4 +805,45 @@ namespace AaruBenchmark public void AaruNative() => throw new NotImplementedException(); #endif } + + [SimpleJob(RuntimeMoniker.NetCoreApp31, baseline: true)] + [Core31RosettaJob] + [Core31WoA] + [Core31Arm] + [SimpleJob(RuntimeMoniker.Net80)] + [SimpleJob(RuntimeMoniker.NativeAot80)] + [MemoryDiagnoser(false)] + [HideColumns("Job", "Error", "StdDev", "Median", "RatioSD", "Alloc Ratio")] + [SuppressMessage("ReSharper", "ConvertClosureToMethodGroup")] + [SuppressMessage("ReSharper", "LambdaExpressionCanBeMadeStatic")] + [SuppressMessage("ReSharper", "MemberCanBeMadeStatic.Local")] + + // Windows only + // [NativeMemoryProfiler] + public class LambdaBenchs + { + static readonly IEnumerable ListOfInts = Enumerable.Range(1, 10_000).ToList(); + + [Benchmark] + public int Lambda_Static() => ListOfInts.Count(x => ConditionStatic(x)); + + [Benchmark] + public int MethodGroup_Static() => ListOfInts.Count(ConditionStatic); + + static bool ConditionStatic(int value) => value > 10; + + [Benchmark] + public int Lambda_Instance() => ListOfInts.Count(x => ConditionInstance(x)); + + [Benchmark] + public int MethodGroup_Instance() => ListOfInts.Count(ConditionInstance); + + bool ConditionInstance(int value) => value > 10; + + [Benchmark] + public int Anonymous_Instance() => ListOfInts.Count(x => x > 10); + + [Benchmark] + public int Anonymous_Static() => ListOfInts.Count(static x => x > 10); + } } \ No newline at end of file diff --git a/AaruBenchmark/Program.cs b/AaruBenchmark/Program.cs index c08bd3d..26c14d2 100644 --- a/AaruBenchmark/Program.cs +++ b/AaruBenchmark/Program.cs @@ -43,6 +43,7 @@ namespace AaruBenchmark BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); + BenchmarkRunner.Run(config); } } } \ No newline at end of file