diff --git a/EvenPerfectBitScanner.Benchmarks/Mod10Benchmarks.cs b/EvenPerfectBitScanner.Benchmarks/Mod10Benchmarks.cs new file mode 100644 index 0000000..90a18d6 --- /dev/null +++ b/EvenPerfectBitScanner.Benchmarks/Mod10Benchmarks.cs @@ -0,0 +1,32 @@ +using System.Numerics; +using BenchmarkDotNet.Attributes; +using PerfectNumbers.Core; + +namespace EvenPerfectBitScanner.Benchmarks; + +[MemoryDiagnoser] +public class Mod10Benchmarks +{ + [Params(3UL, 8191UL, 131071UL, 2147483647UL)] + public ulong Value { get; set; } + + [Benchmark(Baseline = true)] + public ulong LegacyModulo() + { + ulong value = Value; + UInt128 zero = UInt128.Zero; + if (value == zero) + { + return 0UL; + } + + return value % 10UL; + } + + [Benchmark] + public ulong ModMethodModulo() + { + return Value.Mod10(); + } +} + diff --git a/PerfectNumbers.Core/ULongExtensions.cs b/PerfectNumbers.Core/ULongExtensions.cs index 5073c0e..25b4ae6 100644 --- a/PerfectNumbers.Core/ULongExtensions.cs +++ b/PerfectNumbers.Core/ULongExtensions.cs @@ -124,7 +124,7 @@ public static class ULongExtensions } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong Mod10(ulong value) + public static ulong Mod10(this ulong value) { ulong quotient = (ulong)(((UInt128)value * 0xCCCCCCCCCCCCCCCDUL) >> 67); return value - quotient * 10UL;