From 0961dc4f6b04b48c68a317f6f7faf24d3f80ad5e Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 24 Jun 2023 00:50:50 +0200 Subject: [PATCH] Integral comparison benchmarks added --- .../LessOperatorvsAndOperatorBenchmarks.cs | 49 +++++++++++++++++++ Recyclable.Collections.Benchmarks/Program.cs | 1 + 2 files changed, 50 insertions(+) create mode 100644 Recyclable.Collections.Benchmarks/POC/LessOperatorvsAndOperatorBenchmarks.cs diff --git a/Recyclable.Collections.Benchmarks/POC/LessOperatorvsAndOperatorBenchmarks.cs b/Recyclable.Collections.Benchmarks/POC/LessOperatorvsAndOperatorBenchmarks.cs new file mode 100644 index 0000000..3683729 --- /dev/null +++ b/Recyclable.Collections.Benchmarks/POC/LessOperatorvsAndOperatorBenchmarks.cs @@ -0,0 +1,49 @@ +using BenchmarkDotNet.Attributes; +using System.Runtime.CompilerServices; + +namespace Recyclable.Collections.Benchmarks.POC +{ + public class LessOperatorVsAndOperatorBenchmarks + { + private const int bitShift = 1 << 31; + private static Random _rnd = new(1); + private static int num = -Math.Abs(_rnd.Next()); + + public void Setup() + { + num = -1; + Console.WriteLine($"*** We're comparing if {num} is < 0 ***"); + } + + public void Cleanup() { } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static void DoNothing(T item) + { } + + + [Benchmark(Baseline = true)] + public void CompareWithLess() + { + DoNothing(num < 0); + } + + [Benchmark] + public void CompareWithAnd() + { + DoNothing((num & bitShift) != 0); + } + + [Benchmark] + public void CompareWithEqual() + { + DoNothing(num == -1); + } + + [Benchmark] + public void CompareToZero() + { + DoNothing(num == 0); + } + } +} diff --git a/Recyclable.Collections.Benchmarks/Program.cs b/Recyclable.Collections.Benchmarks/Program.cs index 14f5bbd..ec37549 100644 --- a/Recyclable.Collections.Benchmarks/Program.cs +++ b/Recyclable.Collections.Benchmarks/Program.cs @@ -46,6 +46,7 @@ public static class Program _ = BenchmarkRunner.Run(BenchmarkConfig); _ = BenchmarkRunner.Run(BenchmarkConfig); _ = BenchmarkRunner.Run(BenchmarkConfig); + _ = BenchmarkRunner.Run(BenchmarkConfig); // ArraySizeLimitPocBenchmarks>(BenchmarkConfig); }