42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using System.Runtime.CompilerServices;
|
|
using BenchmarkDotNet.Attributes;
|
|
using BenchmarkDotNet.Jobs;
|
|
using PerfectNumbers.Core;
|
|
|
|
namespace EvenPerfectBitScanner.Benchmarks;
|
|
|
|
[SimpleJob(RuntimeMoniker.Net80, launchCount: 1, warmupCount: 1, iterationCount: 5)]
|
|
[MemoryDiagnoser]
|
|
public class Mod3ULongBenchmarks
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static ulong Mod3(ulong value) =>
|
|
((uint)(value & ULongExtensions.WordBitMask)
|
|
+ (uint)((value >> 16) & ULongExtensions.WordBitMask)
|
|
+ (uint)((value >> 32) & ULongExtensions.WordBitMask)
|
|
+ (uint)((value >> 48) & ULongExtensions.WordBitMask)) % 3UL;
|
|
|
|
[Params(3UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 17UL)]
|
|
public ulong Value { get; set; }
|
|
|
|
/// <summary>
|
|
/// `% 3` baseline; delivered 0.259-0.286 ns for the standard values (0.2640 ns at 3, 0.2599 ns at 8191, 0.2620 ns at 131071,
|
|
/// 0.2858 ns at 2147483647) and 0.4916 ns near ulong.Max.
|
|
/// </summary>
|
|
[Benchmark(Baseline = true)]
|
|
public ulong ModuloOperator()
|
|
{
|
|
return Value % 3UL;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Byte-slice helper; measured 0.441 ns at value 3, 0.437 ns at 8191, 0.438 ns at 131071, 0.436 ns at 2147483647, and 0.448 ns
|
|
/// near ulong.Max—about 1.5-1.7x slower than the `%` baseline but still sub-nanosecond.
|
|
/// </summary>
|
|
[Benchmark]
|
|
public ulong ExtensionMethod()
|
|
{
|
|
return Mod3(Value);
|
|
}
|
|
}
|