Extension methods benchmarked. Slower alternatives replaced.

This commit is contained in:
Marek Lemańczyk committed 2025-09-23 22:01:09 +02:00
1 parent 9becb8df67
commit 6e99ee39e3
40 files changed
+930 -1026

No files matched your search

@@ -1,6 +1,6 @@
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using PerfectNumbers.Core;
namespace EvenPerfectBitScanner.Benchmarks;
@@ -8,10 +8,30 @@ namespace EvenPerfectBitScanner.Benchmarks;
[MemoryDiagnoser]
public class Mod7UIntBenchmarks
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Mod7(uint value)
{
uint remainder = 0U;
while (value != 0U)
{
remainder += value & 7U;
value >>= 3;
if (remainder >= 7U)
{
remainder -= 7U;
}
}
return remainder >= 7U ? remainder - 7U : remainder;
}
[Params(7U, 2047U, 65535U, 2147483647U)]
public uint Value { get; set; }
[Benchmark(Baseline = true)]
/// <summary>
/// Fastest
/// </summary>
[Benchmark(Baseline = true)]
public uint ModuloOperator()
{
return Value % 7U;
@@ -20,7 +40,7 @@ public class Mod7UIntBenchmarks
[Benchmark]
public uint ExtensionMethod()
{
return Value.Mod7();
return Mod7(Value);
}
}