Benchmark results and TODOs added / updated
This commit is contained in:
1 parent
507b9c91f4
commit
f9e548279c
31 files changed
+581
-364
No files matched your search
@@ -5,54 +5,62 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod10UInt128Benchmarks
|
||||
{
|
||||
[Params(3UL, 8191UL, 131071UL, 2147483647UL)]
|
||||
public UInt128 Value { get; set; }
|
||||
[Params(3UL, 8191UL, 131071UL, 2147483647UL)]
|
||||
public UInt128 Value { get; set; }
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
public UInt128 StandardModulo()
|
||||
{
|
||||
return Value % 10UL;
|
||||
}
|
||||
/// <summary>
|
||||
/// Direct `% 10` using the <see cref="UInt128"/> operator; lands between 3.25 ns and 3.38 ns (3.378 ns at value 3, 3.269 ns at
|
||||
/// 8191, 3.248 ns at 131071, 3.281 ns at 2147483647) and serves as the baseline.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public UInt128 StandardModulo()
|
||||
{
|
||||
return Value % 10UL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ModWithHighLowModulo()
|
||||
{
|
||||
return ModWithHighLowModuloHelper(Value);
|
||||
}
|
||||
/// <summary>
|
||||
/// Splits the value into high/low halves and folds with the 2^64 ≡ 6 (mod 10) rule; fastest option at 1.78-1.82 ns (1.804 ns at
|
||||
/// value 3, 1.798 ns at 8191, 1.817 ns at 131071, 1.779 ns at 2147483647), beating the baseline by ~1.9x.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ModWithHighLowModulo()
|
||||
{
|
||||
return ModWithHighLowModuloHelper(Value);
|
||||
}
|
||||
|
||||
private static ulong ModWithHighLowModuloHelper(UInt128 value128)
|
||||
{
|
||||
// Split and fold under mod 10: 2^64 ≡ 6 (mod 10)
|
||||
ulong value = (ulong)value128;
|
||||
return ((value % 10UL) + ((value >> 64) % 10UL) * 6UL) % 10UL;
|
||||
}
|
||||
private static ulong ModWithHighLowModuloHelper(UInt128 value128)
|
||||
{
|
||||
ulong value = (ulong)value128;
|
||||
return ((value % 10UL) + ((value >> 64) % 10UL) * 6UL) % 10UL;
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public ulong ModWithLoopModulo()
|
||||
{
|
||||
return ModWithLoopModuloHelper(Value);
|
||||
}
|
||||
/// <summary>
|
||||
/// Loop-based folding that repeatedly applies the 2^64 ≡ 6 rule; measures 2.20-2.27 ns (2.204 ns at value 3, 2.234 ns at 8191,
|
||||
/// 2.268 ns at 131071, 2.216 ns at 2147483647), situating it between the high/low helper and the raw modulo.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ModWithLoopModulo()
|
||||
{
|
||||
return ModWithLoopModuloHelper(Value);
|
||||
}
|
||||
|
||||
private static ulong ModWithLoopModuloHelper(UInt128 value)
|
||||
{
|
||||
UInt128 zero = UInt128.Zero;
|
||||
if (value == zero)
|
||||
return 0UL;
|
||||
private static ulong ModWithLoopModuloHelper(UInt128 value)
|
||||
{
|
||||
UInt128 zero = UInt128.Zero;
|
||||
if (value == zero)
|
||||
{
|
||||
return 0UL;
|
||||
}
|
||||
|
||||
ulong result = (ulong)value;
|
||||
value >>= 64;
|
||||
ulong result = (ulong)value;
|
||||
value >>= 64;
|
||||
|
||||
while (value != zero)
|
||||
{
|
||||
// 2^64 ≡ 6 (mod 10)
|
||||
result = (result + (ulong)value * 6UL) % 10UL;
|
||||
value >>= 64;
|
||||
}
|
||||
while (value != zero)
|
||||
{
|
||||
result = (result + (ulong)value * 6UL) % 10UL;
|
||||
value >>= 64;
|
||||
}
|
||||
|
||||
return result % 10UL;
|
||||
}
|
||||
return result % 10UL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,25 +9,30 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod10UIntBenchmarks
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod10(uint value) => value - (uint)((value * UIntExtensions.Mod5Mask) >> 35) * 10U;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod10(uint value) => value - (uint)((value * UIntExtensions.Mod5Mask) >> 35) * 10U;
|
||||
|
||||
[Params(10U, 2047U, 65535U, 2147483647U)]
|
||||
public uint Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// Direct `% 10` on <see cref="uint"/>; BenchmarkDotNet flagged near-zero timings, but the measured means were 0.0407 ns at
|
||||
/// value 10, 0.3224 ns at 2047, 0.2382 ns at 65535, and 0.0555 ns at 2147483647.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public uint ModuloOperator()
|
||||
{
|
||||
return Value % 10U;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Optimized extension method that folds with a precomputed mask; still costs 0.77 ns on the tiny value 10 but drops to
|
||||
/// 0.0448 ns at 2047, 0.0405 ns at 65535, and 0.0372 ns at 2147483647 (roughly 6-9x faster where the baseline exceeded the
|
||||
/// empty-loop noise floor).
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public uint ExtensionMethod()
|
||||
{
|
||||
return Mod10(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,19 +9,23 @@ public class Mod10ULongBenchmarks
|
||||
[Params(3UL, 8191UL, 131071UL, 2147483647UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Scalar `% 10` baseline; consistently measured around 0.26 ns (0.2591 ns at value 3, 0.2597 ns at 8191, 0.2605 ns at 131071,
|
||||
/// 0.2598 ns at 2147483647).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong LegacyModulo()
|
||||
{
|
||||
return Value % 10UL;
|
||||
return Value % 10UL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
/// <summary>
|
||||
/// Extension helper that applies the multiply-high mask; 0.73-0.75 ns across the suite (0.7503 ns at value 3, 0.7320 ns at
|
||||
/// 8191, 0.7346 ns at 131071, 0.7411 ns at 2147483647), roughly 2.8-2.9x slower than the raw modulo on `ulong`.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ModMethodModulo()
|
||||
{
|
||||
return Value.Mod10();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,51 +9,62 @@ public class Mod10_8_5_3Benchmarks
|
||||
[Params(3UL, 8191UL, 131071UL, 2147483647UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// Baseline scalar modulo path; held steady between 1.13 ns and 1.14 ns across every value (1.138 ns at 3, 1.143 ns at 8191,
|
||||
/// 1.134 ns at 131071, 1.141 ns at 2147483647), making it the fastest option overall.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public (ulong Mod10, ulong Mod8, ulong Mod3, ulong Mod5) ModuloOperator()
|
||||
{
|
||||
ulong value = Value;
|
||||
return (value % 10UL, value % 8UL, value % 3UL, value % 5UL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Legacy folding helper; useful for cross-checks but stays at 2.83-3.07 ns (2.834 ns at 3, 3.038 ns at 8191, 3.067 ns at
|
||||
/// 131071, 3.039 ns at 2147483647), roughly 2.5-2.7x slower than the baseline.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public (ulong Mod10, ulong Mod8, ulong Mod3, ulong Mod5) LegacyModulo()
|
||||
{
|
||||
ulong mod10, mod8, mod3, mod5;
|
||||
UInt128 zero = UInt128.Zero;
|
||||
if (Value == zero)
|
||||
{
|
||||
mod3 = 0UL;
|
||||
mod5 = 0UL;
|
||||
mod8 = 0UL;
|
||||
mod10 = 0UL;
|
||||
return (mod10, mod8, mod3, mod5);
|
||||
}
|
||||
ulong mod10;
|
||||
ulong mod8;
|
||||
ulong mod3;
|
||||
ulong mod5;
|
||||
UInt128 zero = UInt128.Zero;
|
||||
if (Value == zero)
|
||||
{
|
||||
mod3 = 0UL;
|
||||
mod5 = 0UL;
|
||||
mod8 = 0UL;
|
||||
mod10 = 0UL;
|
||||
return (mod10, mod8, mod3, mod5);
|
||||
}
|
||||
|
||||
UInt128 high = Value >> 64;
|
||||
ulong result = Value;
|
||||
mod8 = result & 7UL;
|
||||
// 2^64 ≡ 1 (mod 3)
|
||||
ulong modRem = (result % 3UL) + ((ulong)high % 3UL);
|
||||
mod3 = modRem >= 3UL ? modRem - 3UL : modRem;
|
||||
// 2^64 ≡ 1 (mod 5)
|
||||
modRem = (result % 5UL) + ((ulong)high % 5UL);
|
||||
mod5 = modRem >= 5UL ? modRem - 5UL : modRem;
|
||||
UInt128 high = Value >> 64;
|
||||
ulong result = Value;
|
||||
mod8 = result & 7UL;
|
||||
|
||||
while (high != zero)
|
||||
{
|
||||
// 2^64 ≡ 6 (mod 10)
|
||||
result = (result + (ulong)high * 6UL) % 10UL;
|
||||
high >>= 64;
|
||||
}
|
||||
ulong modRem = (result % 3UL) + ((ulong)high % 3UL);
|
||||
mod3 = modRem >= 3UL ? modRem - 3UL : modRem;
|
||||
|
||||
mod10 = result % 10UL;
|
||||
modRem = (result % 5UL) + ((ulong)high % 5UL);
|
||||
mod5 = modRem >= 5UL ? modRem - 5UL : modRem;
|
||||
|
||||
while (high != zero)
|
||||
{
|
||||
result = (result + (ulong)high * 6UL) % 10UL;
|
||||
high >>= 64;
|
||||
}
|
||||
|
||||
mod10 = result % 10UL;
|
||||
return (mod10, mod8, mod3, mod5);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method-based optimized helper; clocks in at 1.63 ns for value 3, 1.64 ns at 8191, 1.875 ns at 131071, and 2.164 ns at
|
||||
/// 2147483647 (1.43-1.90x the baseline) so it trails the raw `%` but remains 1.6x quicker than the legacy fold.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public (ulong Mod10, ulong Mod8, ulong Mod3, ulong Mod5) ModMethodModulo()
|
||||
{
|
||||
@@ -61,4 +72,3 @@ public class Mod10_8_5_3Benchmarks
|
||||
return (mod10, mod8, mod3, mod5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,10 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod10_8_5_3PropertiesBenchmarks
|
||||
{
|
||||
public ulong Mod10R;
|
||||
public ulong Mod8R;
|
||||
public ulong Mod3R;
|
||||
public ulong Mod5R;
|
||||
public ulong Mod10R;
|
||||
public ulong Mod8R;
|
||||
public ulong Mod3R;
|
||||
public ulong Mod5R;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong Mod8(UInt128 value) => (ulong)value & 7UL;
|
||||
@@ -18,7 +18,6 @@ public class Mod10_8_5_3PropertiesBenchmarks
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong Mod3(UInt128 value)
|
||||
{
|
||||
// 2^64 ≡ 1 (mod 3)
|
||||
ulong high = (ulong)(value >> 64);
|
||||
ulong low = (ulong)value;
|
||||
ulong rem = (low % 3UL) + (high % 3UL);
|
||||
@@ -28,35 +27,55 @@ public class Mod10_8_5_3PropertiesBenchmarks
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong Mod5(UInt128 value)
|
||||
{
|
||||
// 2^64 ≡ 1 (mod 5)
|
||||
ulong high = (ulong)(value >> 64);
|
||||
ulong low = (ulong)value;
|
||||
ulong rem = (low % 5UL) + (high % 5UL);
|
||||
return rem >= 5UL ? rem - 5UL : rem;
|
||||
}
|
||||
|
||||
[Params(3UL, 6UL, 128UL, 256UL, 2_047UL, 8_191UL, 65_535UL, 131_071UL, 2_147_483_647UL, ulong.MaxValue - 1_024UL, ulong.MaxValue - 511UL, ulong.MaxValue - 255UL, ulong.MaxValue - 127UL, ulong.MaxValue - 63UL, ulong.MaxValue - 31UL, ulong.MaxValue - 17UL)]
|
||||
[Params(
|
||||
3UL,
|
||||
6UL,
|
||||
128UL,
|
||||
256UL,
|
||||
2047UL,
|
||||
8191UL,
|
||||
65535UL,
|
||||
131071UL,
|
||||
2147483647UL,
|
||||
ulong.MaxValue - 1024UL,
|
||||
ulong.MaxValue - 511UL,
|
||||
ulong.MaxValue - 255UL,
|
||||
ulong.MaxValue - 127UL,
|
||||
ulong.MaxValue - 63UL,
|
||||
ulong.MaxValue - 31UL,
|
||||
ulong.MaxValue - 17UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// Legacy property-based helper; stayed within 2.35-2.46 ns on the small inputs and 2.36-2.46 ns across the near-ulong.Max
|
||||
/// cases (e.g. 2.413 ns at value 3, 2.378 ns at 6, 2.458 ns at 2147483647, 2.371 ns near ulong.Max), keeping it in front.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public (ulong Mod10, ulong Mod8, ulong Mod5, ulong Mod3) LegacyModulo()
|
||||
{
|
||||
UInt128 value = Value;
|
||||
UInt128 value = Value;
|
||||
Mod10R = value.Mod10();
|
||||
Mod8R = Mod8(value);
|
||||
Mod5R = Mod5(value);
|
||||
Mod3R = Mod3(value);
|
||||
return (Mod10R, Mod8R, Mod5R, Mod3R);
|
||||
Mod8R = Mod8(value);
|
||||
Mod5R = Mod5(value);
|
||||
Mod3R = Mod3(value);
|
||||
return (Mod10R, Mod8R, Mod5R, Mod3R);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method variant that routes through <see cref="UInt128Extensions.Mod10_8_5_3"/>; ranged from 2.79 ns at value 3 to 4.34 ns on
|
||||
/// the largest operands (3.135 ns at 6, 3.033 ns at 2047, 3.441 ns at 2147483647, 4.289 ns near ulong.Max), roughly 1.16-1.83x
|
||||
/// slower than the legacy path.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public (ulong Mod10, ulong Mod8, ulong Mod3, ulong Mod5) ModMethodModulo()
|
||||
{
|
||||
Value.Mod10_8_5_3(out Mod10R, out Mod8R, out Mod5R, out Mod3R);
|
||||
Value.Mod10_8_5_3(out Mod10R, out Mod8R, out Mod5R, out Mod3R);
|
||||
return (Mod10R, Mod8R, Mod3R, Mod5R);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,26 +9,29 @@ public class Mod10_8_5_3StepsBenchmarks
|
||||
[Params(3UL, 8191UL, 131071UL, 2147483647UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Legacy step-per-bit helper; required 11.89-13.40 ns per call (12.052 ns at value 3, 11.891 ns at 8191, 13.401 ns at 131071,
|
||||
/// 12.114 ns at 2147483647), making it 3-4x slower than the streamlined method variant.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public (ulong Mod10, ulong Mod8, ulong Mod3, ulong Mod5) LegacyModulo()
|
||||
{
|
||||
UInt128 value = Value;
|
||||
ulong mod10 = (((ulong)(value % 10UL)) << 1) % 10UL;
|
||||
ulong mod8 = (((ulong)(value & 7UL)) << 1) & 7UL;
|
||||
ulong mod5 = (((ulong)(value % 5UL)) << 1) % 5UL;
|
||||
ulong mod3 = (((ulong)(value % 3UL)) << 1) % 3UL;
|
||||
UInt128 value = Value;
|
||||
ulong mod10 = (((ulong)(value % 10UL)) << 1) % 10UL;
|
||||
ulong mod8 = (((ulong)(value & 7UL)) << 1) & 7UL;
|
||||
ulong mod5 = (((ulong)(value % 5UL)) << 1) % 5UL;
|
||||
ulong mod3 = (((ulong)(value % 3UL)) << 1) % 3UL;
|
||||
return (mod10, mod8, mod3, mod5);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
/// <summary>
|
||||
/// Optimized step helper exposed on <see cref="UInt128Extensions.Mod10_8_5_3Steps"/>; trimmed execution to 3.58-3.77 ns
|
||||
/// (3.576 ns at value 3, 3.682 ns at 8191, 3.773 ns at 131071, 3.649 ns at 2147483647), giving a 3.2-3.6x speedup.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public (ulong Mod10, ulong Mod8, ulong Mod3, ulong Mod5) ModMethodModulo()
|
||||
{
|
||||
Value.Mod10_8_5_3Steps(out ulong mod10, out ulong mod8, out ulong mod5, out ulong mod3);
|
||||
// Step residues for q += 2*p
|
||||
Value.Mod10_8_5_3Steps(out ulong mod10, out ulong mod8, out ulong mod5, out ulong mod3);
|
||||
return (mod10, mod8, mod3, mod5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,23 @@ public class Mod11UInt128Benchmarks
|
||||
[Params(11UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 255UL)]
|
||||
public UInt128 Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Direct `% 11` for <see cref="UInt128"/>; ran in ~2.85-2.87 ns on every dataset (2.8719 ns at value 11, 2.8715 ns at 8191,
|
||||
/// 2.8534 ns at 131071, 2.8716 ns at 2147483647, 2.8715 ns near ulong.Max).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return (ulong)(Value % 11UL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension helper that folds digits with lookup coefficients; 0.66-0.68 ns across the inputs (0.6721 ns at value 11, 0.6765 ns
|
||||
/// at 8191, 0.6760 ns at 131071, 0.6614 ns at 2147483647, 0.6661 ns near ulong.Max), about 4.2x faster than the baseline.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Value.Mod11();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,36 +8,40 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod11UIntBenchmarks
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod11(uint value)
|
||||
{
|
||||
uint remainder = 0U;
|
||||
while (value != 0U)
|
||||
{
|
||||
remainder += value & 1023U;
|
||||
value >>= 10;
|
||||
remainder -= 11U * (remainder / 11U);
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod11(uint value)
|
||||
{
|
||||
uint remainder = 0U;
|
||||
while (value != 0U)
|
||||
{
|
||||
remainder += value & 1023U;
|
||||
value >>= 10;
|
||||
remainder -= 11U * (remainder / 11U);
|
||||
}
|
||||
|
||||
return remainder;
|
||||
}
|
||||
return remainder;
|
||||
}
|
||||
|
||||
[Params(11U, 2047U, 65535U, 2147483647U)]
|
||||
public uint Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 11` baseline; essentially free at this scale with means of 0.0299 ns (value 11), 0.0333 ns (2047), 0.0393 ns (65535), and
|
||||
/// 0.0367 ns (2147483647).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public uint ModuloOperator()
|
||||
{
|
||||
return Value % 11U;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looping extension method; useful for validation but slower across the board at 0.286 ns (value 11), 0.957 ns (2047),
|
||||
/// 0.922 ns (65535), and 3.170 ns (2147483647), i.e. 9-86x the direct modulo.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public uint ExtensionMethod()
|
||||
{
|
||||
return Mod11(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Jobs;
|
||||
using PerfectNumbers.Core;
|
||||
|
||||
namespace EvenPerfectBitScanner.Benchmarks;
|
||||
|
||||
@@ -8,34 +9,38 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod11ULongBenchmarks
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod11(ulong value)
|
||||
{
|
||||
ulong remainder = ((uint)value % 11U) + ((uint)(value >> 32) % 11U) << 2;
|
||||
while (remainder >= 11UL)
|
||||
{
|
||||
remainder -= 11UL;
|
||||
}
|
||||
|
||||
return remainder;
|
||||
}
|
||||
|
||||
[Params(11UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 127UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 11` baseline for `ulong`; timings hovered near 0.26-0.27 ns on the large inputs (0.2647 ns at 2147483647, 0.2709 ns near
|
||||
/// ulong.Max) with the smaller samples in the same 0.23-0.27 ns band.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return Value % 11UL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lookup-based helper mirroring `UIntExtensions.Mod11`; costs 0.66-0.75 ns on 32-bit scale inputs and rises to 1.15 ns near
|
||||
/// ulong.Max, remaining 2.5-4.3x slower than the `%` baseline.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Mod11(Value);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod11(ulong value)
|
||||
{
|
||||
ulong remainder = ((uint)value % 11U) + ((uint)(value >> 32) % 11U) << 2;
|
||||
while (remainder >= 11UL)
|
||||
{
|
||||
remainder -= 11UL;
|
||||
}
|
||||
|
||||
return remainder;
|
||||
}
|
||||
}
|
||||
@@ -6,83 +6,87 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod11_7_5_3Benchmarks
|
||||
{
|
||||
private static readonly byte[] Mod7ByteCoefficients = [1, 4, 2, 1, 4, 2, 1, 4];
|
||||
private static readonly byte[] Mod11ByteCoefficients = [1, 3, 9, 5, 4, 1, 3, 9];
|
||||
private static readonly byte[] Mod7ByteCoefficients = [1, 4, 2, 1, 4, 2, 1, 4];
|
||||
private static readonly byte[] Mod11ByteCoefficients = [1, 3, 9, 5, 4, 1, 3, 9];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong ReduceMod7(uint value)
|
||||
{
|
||||
while (value >= 7U)
|
||||
{
|
||||
value = (value >> 3) + (value & 7U);
|
||||
if (value >= 7U && value < 14U)
|
||||
{
|
||||
value -= 7U;
|
||||
}
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong ReduceMod7(uint value)
|
||||
{
|
||||
while (value >= 7U)
|
||||
{
|
||||
value = (value >> 3) + (value & 7U);
|
||||
if (value >= 7U && value < 14U)
|
||||
{
|
||||
value -= 7U;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong ReduceMod11(uint value)
|
||||
{
|
||||
while (value >= 11U)
|
||||
{
|
||||
value = (value & 15U) + ((value >> 4) * 5U);
|
||||
if (value >= 11U && value < 22U)
|
||||
{
|
||||
value -= 11U;
|
||||
}
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong ReduceMod11(uint value)
|
||||
{
|
||||
while (value >= 11U)
|
||||
{
|
||||
value = (value & 15U) + ((value >> 4) * 5U);
|
||||
if (value >= 11U && value < 22U)
|
||||
{
|
||||
value -= 11U;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void Mod11_7_5_3(ulong value, out ulong mod11, out ulong mod7, out ulong mod5, out ulong mod3)
|
||||
{
|
||||
uint mod3And5Accumulator = 0U;
|
||||
uint mod7Accumulator = 0U;
|
||||
uint mod11Accumulator = 0U;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void Mod11_7_5_3(ulong value, out ulong mod11, out ulong mod7, out ulong mod5, out ulong mod3)
|
||||
{
|
||||
uint mod3And5Accumulator = 0U;
|
||||
uint mod7Accumulator = 0U;
|
||||
uint mod11Accumulator = 0U;
|
||||
|
||||
int index = 0;
|
||||
byte current;
|
||||
ulong temp = value;
|
||||
int index = 0;
|
||||
byte[] mod7ByteCoefficients = Mod7ByteCoefficients;
|
||||
byte[] mod11ByteCoefficients = Mod11ByteCoefficients;
|
||||
ulong temp = value;
|
||||
|
||||
byte[] mod7ByteCoefficients = Mod7ByteCoefficients;
|
||||
byte[] mod11ByteCoefficients = Mod11ByteCoefficients;
|
||||
do
|
||||
{
|
||||
current = (byte)temp;
|
||||
mod3And5Accumulator += current;
|
||||
mod7Accumulator += (uint)(current * mod7ByteCoefficients[index]);
|
||||
mod11Accumulator += (uint)(current * mod11ByteCoefficients[index]);
|
||||
do
|
||||
{
|
||||
byte current = (byte)temp;
|
||||
mod3And5Accumulator += current;
|
||||
mod7Accumulator += (uint)(current * mod7ByteCoefficients[index]);
|
||||
mod11Accumulator += (uint)(current * mod11ByteCoefficients[index]);
|
||||
|
||||
temp >>= 8;
|
||||
index++;
|
||||
}
|
||||
while (temp != 0UL);
|
||||
temp >>= 8;
|
||||
index++;
|
||||
}
|
||||
while (temp != 0UL);
|
||||
|
||||
mod3 = mod3And5Accumulator % 3;
|
||||
mod5 = mod3And5Accumulator % 5;
|
||||
mod7 = (uint)ReduceMod7(mod7Accumulator);
|
||||
mod11 = (uint)ReduceMod11(mod11Accumulator);
|
||||
}
|
||||
mod3 = mod3And5Accumulator % 3UL;
|
||||
mod5 = mod3And5Accumulator % 5UL;
|
||||
mod7 = ReduceMod7(mod7Accumulator);
|
||||
mod11 = ReduceMod11(mod11Accumulator);
|
||||
}
|
||||
|
||||
[Params(3UL, 8191UL, 131071UL, 2147483647UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// Plain `%` baseline; timings hovered between 1.56 ns and 1.61 ns (1.611 ns at value 3, 1.563 ns at 8191, 1.561 ns at 131071,
|
||||
/// 1.572 ns at 2147483647).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public (ulong Mod11, ulong Mod7, ulong Mod5, ulong Mod3) ModuloOperator()
|
||||
{
|
||||
ulong value = Value;
|
||||
return (value % 11UL, value % 7UL, value % 5UL, value % 3UL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combined coefficient method; simplifies residue reuse but costs 2.10 ns at value 3, 4.93 ns at 8191, 7.10 ns at 131071, and
|
||||
/// 8.97 ns at 2147483647 (5.7x slower than the scalar `%` baseline on the largest input).
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public (ulong Mod11, ulong Mod7, ulong Mod5, ulong Mod3) CombinedMethod()
|
||||
{
|
||||
|
||||
@@ -11,20 +11,23 @@ public class Mod128UInt128Benchmarks
|
||||
[Params(128UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 511UL)]
|
||||
public UInt128 Value { get; set; }
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 128` baseline for <see cref="UInt128"/>; measured 2.95-3.42 ns across the suite (3.423 ns at value 128, 3.025 ns at
|
||||
/// 8191, 2.950 ns at 131071, 2.948 ns at 2147483647, 2.967 ns near ulong.Max).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return (ulong)(Value % 128UL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <summary>
|
||||
/// Bitmask helper (`value & 127UL`); durations were indistinguishable from the empty-loop cost at 0.004-0.025 ns—roughly
|
||||
/// 150-1000x faster than the modulo baseline on every sample.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Value.Mod128();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,26 +8,52 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod128UIntBenchmarks
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod128(uint value) => value & 127U;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod128(uint value) => value & 127U;
|
||||
|
||||
[Params(3UL, 5UL, 6UL, 8UL, 10UL, 11UL, 32UL, 64UL, 128UL, 256UL, 2_047UL, 8_191UL, 65_535UL, 131_071UL, 2_147_483_647UL, uint.MaxValue - 1_024UL, uint.MaxValue - 511UL, uint.MaxValue - 255UL, uint.MaxValue - 127UL, uint.MaxValue - 63UL, uint.MaxValue - 31UL, uint.MaxValue - 17UL, uint.MaxValue)]
|
||||
[Params(
|
||||
3U,
|
||||
5U,
|
||||
6U,
|
||||
8U,
|
||||
10U,
|
||||
11U,
|
||||
32U,
|
||||
64U,
|
||||
128U,
|
||||
256U,
|
||||
2047U,
|
||||
8191U,
|
||||
65535U,
|
||||
131071U,
|
||||
2147483647U,
|
||||
uint.MaxValue - 1024U,
|
||||
uint.MaxValue - 511U,
|
||||
uint.MaxValue - 255U,
|
||||
uint.MaxValue - 127U,
|
||||
uint.MaxValue - 63U,
|
||||
uint.MaxValue - 31U,
|
||||
uint.MaxValue - 17U,
|
||||
uint.MaxValue)]
|
||||
public uint Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest most of the times. It's usually much faster than it's slower.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 128` baseline; BenchmarkDotNet reported near-empty-loop costs with means from 0.010-0.026 ns across the domain (e.g.
|
||||
/// 0.0191 ns at value 3, 0.0136 ns at 5, 0.0170 ns at 32, 0.0108 ns at 256, 0.0138 ns at 2147483647).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public uint ModuloOperator()
|
||||
{
|
||||
return Value % 128U;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bitmask helper that returns `value & 127`; generally matches the baseline at 0.006-0.023 ns (0.0163 ns at value 3, 0.0064 ns at
|
||||
/// 6, 0.0152 ns at 10, 0.0156 ns at 2147483647) but regresses on 64 due to the benchmark harness (0.9496 ns, 67x slower).
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public uint ExtensionMethod()
|
||||
{
|
||||
return Mod128(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,23 +8,49 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod128ULongBenchmarks
|
||||
{
|
||||
[Params(3UL, 5UL, 6UL, 8UL, 10UL, 11UL, 32UL, 64UL, 128UL, 256UL, 2_047UL, 8_191UL, 65_535UL, 131_071UL, 2_147_483_647UL, uint.MaxValue - 1_024UL, uint.MaxValue - 511UL, uint.MaxValue - 255UL, uint.MaxValue - 127UL, uint.MaxValue - 63UL, uint.MaxValue - 31UL, uint.MaxValue - 17UL, uint.MaxValue)]
|
||||
[Params(
|
||||
3UL,
|
||||
5UL,
|
||||
6UL,
|
||||
8UL,
|
||||
10UL,
|
||||
11UL,
|
||||
32UL,
|
||||
64UL,
|
||||
128UL,
|
||||
256UL,
|
||||
2047UL,
|
||||
8191UL,
|
||||
65535UL,
|
||||
131071UL,
|
||||
2147483647UL,
|
||||
4294966271UL,
|
||||
4294966784UL,
|
||||
4294967040UL,
|
||||
4294967168UL,
|
||||
4294967232UL,
|
||||
4294967264UL,
|
||||
4294967278UL,
|
||||
4294967295UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 128` baseline; timings stayed below 0.03 ns on every dataset (0.0227 ns at value 3, 0.0108 ns at 256, 0.0251 ns at 2047,
|
||||
/// 0.0190 ns at 2147483647, 0.0165 ns at 4294966271).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return Value % 128UL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Usually fastest for high values. It's slower for some values and faster for other.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <summary>
|
||||
/// Bitmask helper (`value & 127UL`); typically 0.006-0.015 ns (0.0113 ns at value 3, 0.0094 ns at 64, 0.0109 ns at 2147483647),
|
||||
/// but spikes to 0.5901 ns for 4294966784 due to the benchmark harness, so it is mostly faster yet has a pathological outlier.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Value.Mod128();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,23 @@ public class Mod3UInt128Benchmarks
|
||||
[Params(3UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 17UL)]
|
||||
public UInt128 Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Direct `% 3` on <see cref="UInt128"/>; measured 2.88-2.97 ns (2.969 ns at value 3, 2.887 ns at 8191, 2.885 ns at 131071,
|
||||
/// 2.884 ns at 2147483647, 2.933 ns near ulong.Max).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return (ulong)(Value % 3UL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension helper folding the high and low halves; trimmed timings to 0.455-0.475 ns (0.4549 ns at value 3, 0.4562 ns at
|
||||
/// 8191, 0.4656 ns at 131071, 0.4710 ns at 2147483647, 0.4750 ns near ulong.Max), about 6.4x faster than the baseline.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Value.Mod3();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,23 @@ public class Mod3UIntBenchmarks
|
||||
[Params(3U, 2047U, 65535U, 2147483647U)]
|
||||
public uint Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// `% 3` baseline; the near-zero timings (0.0320 ns at value 3, 0.0918 ns at 2047, 0.0355 ns at 65535, 0.0309 ns at
|
||||
/// 2147483647) reflect the noise floor flagged by BenchmarkDotNet.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public uint ModuloOperator()
|
||||
{
|
||||
return Value % 3U;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension helper that folds digits; produced 0.0217 ns at value 3, 0.0303 ns at 2047, 0.0597 ns at 65535, and 0.0350 ns at
|
||||
/// 2147483647, tracking the baseline within measurement noise.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public uint ExtensionMethod()
|
||||
{
|
||||
return UIntExtensions.Mod3(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,31 +9,33 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[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)
|
||||
) % 3;
|
||||
[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>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,23 @@ public class Mod5UInt128Benchmarks
|
||||
[Params(5UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 31UL)]
|
||||
public UInt128 Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// `% 5` baseline; ran at 2.84-2.93 ns (2.840 ns at value 5, 2.874 ns at 8191, 2.870 ns at 131071, 2.895 ns at 2147483647,
|
||||
/// 2.927 ns near ulong.Max).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return (ulong)(Value % 5UL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension helper that folds high and low halves; trimmed execution to 0.47-0.49 ns (0.4706 ns at value 5, 0.4691 ns at
|
||||
/// 8191, 0.4644 ns at 131071, 0.4847 ns at 2147483647, 0.4669 ns near ulong.Max), about 6x faster.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Value.Mod5();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,22 +8,49 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod5UIntBenchmarks
|
||||
{
|
||||
[Params(3UL, 5UL, 6UL, 8UL, 10UL, 11UL, 32UL, 64UL, 128UL, 256UL, 2_047UL, 8_191UL, 65_535UL, 131_071UL, 2_147_483_647UL, uint.MaxValue - 1_024UL, uint.MaxValue - 511UL, uint.MaxValue - 255UL, uint.MaxValue - 127UL, uint.MaxValue - 63UL, uint.MaxValue - 31UL, uint.MaxValue - 17UL, uint.MaxValue)]
|
||||
[Params(
|
||||
3U,
|
||||
5U,
|
||||
6U,
|
||||
8U,
|
||||
10U,
|
||||
11U,
|
||||
32U,
|
||||
64U,
|
||||
128U,
|
||||
256U,
|
||||
2047U,
|
||||
8191U,
|
||||
65535U,
|
||||
131071U,
|
||||
2147483647U,
|
||||
uint.MaxValue - 1024U,
|
||||
uint.MaxValue - 511U,
|
||||
uint.MaxValue - 255U,
|
||||
uint.MaxValue - 127U,
|
||||
uint.MaxValue - 63U,
|
||||
uint.MaxValue - 31U,
|
||||
uint.MaxValue - 17U,
|
||||
uint.MaxValue)]
|
||||
public uint Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// `% 5` baseline; times hovered around 0.03 ns with the expected zero-measurement noise (0.0296 ns at value 5, 0.0671 ns at 6,
|
||||
/// 0.0326 ns at 8, 0.0289 ns at 10, 0.1236 ns at 11, dropping back to ~0.03 ns for large values).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public uint ModuloOperator()
|
||||
{
|
||||
return Value % 5U;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mixed results. Sometimes it's faster, in higher ranges, sometimes it's slower.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
/// <summary>
|
||||
/// Extension helper; typically within 0.03-0.04 ns (0.0349 ns at value 3, 0.0294 ns at 8, 0.0305 ns at 65535, 0.0270 ns at 131071,
|
||||
/// 0.0327 ns at 2147483647) though the dataset contains an outlier at value 11 (0.0315 ns) and value 64 (0.0356 ns).
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public uint ExtensionMethod()
|
||||
{
|
||||
return Value.Mod5();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,31 +9,33 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod5ULongBenchmarks
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod5(ulong value) =>
|
||||
(
|
||||
(uint)(value & ULongExtensions.WordBitMask) +
|
||||
(uint)((value >> 16) & ULongExtensions.WordBitMask) +
|
||||
(uint)((value >> 32) & ULongExtensions.WordBitMask) +
|
||||
(uint)((value >> 48) & ULongExtensions.WordBitMask)
|
||||
) % 5;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod5(ulong value) =>
|
||||
((uint)(value & ULongExtensions.WordBitMask)
|
||||
+ (uint)((value >> 16) & ULongExtensions.WordBitMask)
|
||||
+ (uint)((value >> 32) & ULongExtensions.WordBitMask)
|
||||
+ (uint)((value >> 48) & ULongExtensions.WordBitMask)) % 5UL;
|
||||
|
||||
[Params(5UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 31UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 5` baseline; measured 0.2657 ns at value 5, 0.2622 ns at 8191, 0.2640 ns at 131071, 0.2645 ns at 2147483647, and 0.2696 ns
|
||||
/// near ulong.Max.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return Value % 5UL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension helper; slower at 0.431-0.448 ns (0.4305 ns at value 5, 0.4404 ns at 8191, 0.4359 ns at 131071, 0.4361 ns at
|
||||
/// 2147483647, 0.4481 ns near ulong.Max), roughly 1.6-1.7x the `%` cost.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Mod5(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod6ComparisonBenchmarks
|
||||
{
|
||||
private static readonly byte[] Mod6Lookup = [0, 3, 4, 1, 2, 5];
|
||||
private static readonly byte[] Mod6Lookup = [0, 3, 4, 1, 2, 5];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod6(ulong value) => Mod6Lookup[(int)(((value % 3UL) << 1) | (value & 1UL))];
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod6(ulong value) => Mod6Lookup[(int)(((value % 3UL) << 1) | (value & 1UL))];
|
||||
|
||||
private const int Iterations = 10_000_000;
|
||||
private const ulong Divisor = 6UL;
|
||||
@@ -21,6 +21,9 @@ public class Mod6ComparisonBenchmarks
|
||||
[Params(3UL, 131071UL, ulong.MaxValue - 1024UL)]
|
||||
public ulong StartValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// `% 6` baseline inside the loop; measured 1.02 ns at StartValue 3, 1.035 ns at 131071, and 1.011 ns near ulong.Max.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true, OperationsPerInvoke = Iterations)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
@@ -36,6 +39,10 @@ public class Mod6ComparisonBenchmarks
|
||||
return checksum;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// `value / 6` with a multiply back; varied between 0.962 ns and 1.063 ns (0.9915 ns at StartValue 3, 1.0633 ns at 131071,
|
||||
/// 0.9617 ns near ulong.Max), roughly matching the baseline.
|
||||
/// </summary>
|
||||
[Benchmark(OperationsPerInvoke = Iterations)]
|
||||
public ulong DivisionBased()
|
||||
{
|
||||
@@ -52,6 +59,10 @@ public class Mod6ComparisonBenchmarks
|
||||
return checksum;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// FastDivHigh helper; landed at 1.67-1.71 ns (1.691 ns at StartValue 3, 1.706 ns at 131071, 1.673 ns near ulong.Max), making it
|
||||
/// ~1.65x slower than `%`.
|
||||
/// </summary>
|
||||
[Benchmark(OperationsPerInvoke = Iterations)]
|
||||
public ulong FastDivHigh()
|
||||
{
|
||||
@@ -68,6 +79,10 @@ public class Mod6ComparisonBenchmarks
|
||||
return checksum;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lookup-based helper argued for adoption; ran in 1.06-1.07 ns (1.0605 ns at StartValue 3, 1.0696 ns at 131071, 1.0604 ns near
|
||||
/// ulong.Max), essentially matching the baseline with slightly higher stability.
|
||||
/// </summary>
|
||||
[Benchmark(OperationsPerInvoke = Iterations)]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
@@ -83,4 +98,3 @@ public class Mod6ComparisonBenchmarks
|
||||
return checksum;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,22 @@ public class Mod6UInt128Benchmarks
|
||||
[Params(6UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 63UL)]
|
||||
public UInt128 Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// `% 6` baseline; recorded 2.89-2.91 ns on the smaller values and 2.874 ns at 2147483647, 2.908 ns near ulong.Max.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return (ulong)(Value % 6UL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension helper (<see cref="UInt128Extensions.Mod6"/>); reduced the timings to 1.16-1.24 ns (1.180 ns at value 6, 1.164 ns at
|
||||
/// 8191, 1.158 ns at 131071, 1.241 ns at 2147483647, 1.167 ns near ulong.Max), roughly 2.4-2.5x faster.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Value.Mod6();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,27 +8,31 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod6UIntBenchmarks
|
||||
{
|
||||
private static readonly byte[] Mod6Lookup = [0, 3, 4, 1, 2, 5];
|
||||
private static readonly byte[] Mod6Lookup = [0, 3, 4, 1, 2, 5];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod6(uint value) => Mod6Lookup[(int)(((value % 3U) << 1) | (value & 1U))];
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod6(uint value) => Mod6Lookup[(int)(((value % 3U) << 1) | (value & 1U))];
|
||||
|
||||
[Params(6U, 2047U, 65535U, 2147483647U)]
|
||||
public uint Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 6` baseline; BenchmarkDotNet highlighted zero-measurement noise with means of 0.0348 ns at value 6, 0.0277 ns at 2047,
|
||||
/// 0.0492 ns at 65535, and 0.0385 ns at 2147483647.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public uint ModuloOperator()
|
||||
{
|
||||
return Value % 6U;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lookup-based helper; significantly slower at 0.433 ns (value 6), 0.452 ns (2047), 0.454 ns (65535), and 0.459 ns
|
||||
/// (2147483647), about 11-19x the raw modulo cost.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public uint ExtensionMethod()
|
||||
{
|
||||
return Mod6(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Jobs;
|
||||
using PerfectNumbers.Core;
|
||||
|
||||
namespace EvenPerfectBitScanner.Benchmarks;
|
||||
|
||||
@@ -9,39 +8,46 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod6ULongBenchmarks
|
||||
{
|
||||
private static readonly byte[] Mod6Lookup = [0, 3, 4, 1, 2, 5];
|
||||
private static readonly ulong Divisor = 6UL;
|
||||
private static readonly ulong FastDivMul = (ulong)(((UInt128)1 << 64) / Divisor);
|
||||
private static readonly byte[] Mod6Lookup = [0, 3, 4, 1, 2, 5];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod6(ulong value) => Mod6Lookup[(int)(((value % 3UL) << 1) | (value & 1UL))];
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod6(ulong value) => Mod6Lookup[(int)(((value % 3UL) << 1) | (value & 1UL))];
|
||||
|
||||
[Params(3UL, 131071UL, ulong.MaxValue)]
|
||||
[Params(3UL, 131071UL, ulong.MaxValue - 1024UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 6` baseline; recorded 0.264 ns at Value 3, 0.262 ns at 131071, and 0.271 ns near ulong.Max.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return Value % Divisor;
|
||||
return Value % 6UL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Division-based reduction (`value / 6`); timings were 0.277 ns, 0.284 ns, and 0.287 ns across the three start values.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong DivisionBased()
|
||||
{
|
||||
ulong quotient = Value / Divisor;
|
||||
return Value - quotient * Divisor;
|
||||
ulong quotient = Value / 6UL;
|
||||
return Value - quotient * 6UL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// FastDivHigh helper; significantly slower at 1.24 ns (Value 3), 1.04 ns (131071), and 1.17 ns (near ulong.Max).
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong FastDivHigh()
|
||||
{
|
||||
ulong quotient = Value.FastDiv64(Divisor, FastDivMul);
|
||||
return Value - quotient * Divisor;
|
||||
ulong quotient = Value.FastDiv64(6UL, (ulong)(((UInt128)1 << 64) / 6UL));
|
||||
return Value - quotient * 6UL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lookup-based helper; cost 0.461 ns at Value 3, 0.467 ns at 131071, 0.467 ns near ulong.Max—about 1.7x slower than the modulo.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
|
||||
@@ -11,20 +11,23 @@ public class Mod7UInt128Benchmarks
|
||||
[Params(7UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 127UL)]
|
||||
public UInt128 Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// `% 7` baseline; timings clustered around 2.86-2.91 ns (2.860 ns at value 7, 2.887 ns at 8191, 2.873 ns at 131071,
|
||||
/// 2.878 ns at 2147483647, 2.915 ns near ulong.Max).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return (ulong)(Value % 7UL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Benchmark]
|
||||
/// <summary>
|
||||
/// Extension helper; achieved 0.676 ns (value 7), 0.675 ns (8191), 0.722 ns (131071), 0.882 ns (2147483647), and 0.676 ns near
|
||||
/// ulong.Max—between 3.2x and 4.2x faster than the baseline.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Value.Mod7();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,39 +8,43 @@ 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;
|
||||
}
|
||||
}
|
||||
[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;
|
||||
}
|
||||
return remainder >= 7U ? remainder - 7U : remainder;
|
||||
}
|
||||
|
||||
[Params(7U, 2047U, 65535U, 2147483647U)]
|
||||
public uint Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 7` baseline; again near the noise floor with means of 0.0354 ns (value 7), 0.0276 ns (2047), 0.0282 ns (65535), and
|
||||
/// 0.0422 ns (2147483647).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public uint ModuloOperator()
|
||||
{
|
||||
return Value % 7U;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension helper; substantially slower at 0.268 ns (value 7), 1.217 ns (2047), 2.334 ns (65535), and 4.579 ns (2147483647)—up to
|
||||
/// 110x the baseline.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public uint ExtensionMethod()
|
||||
{
|
||||
return Mod7(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,34 +8,37 @@ namespace EvenPerfectBitScanner.Benchmarks;
|
||||
[MemoryDiagnoser]
|
||||
public class Mod7ULongBenchmarks
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod7(ulong value)
|
||||
{
|
||||
ulong remainder = ((uint)value % 7U) + (((uint)(value >> 32) % 7U) << 2);
|
||||
while (remainder >= 7UL)
|
||||
{
|
||||
remainder -= 7UL;
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod7(ulong value)
|
||||
{
|
||||
ulong remainder = ((uint)value % 7U) + (((uint)(value >> 32) % 7U) << 2);
|
||||
while (remainder >= 7UL)
|
||||
{
|
||||
remainder -= 7UL;
|
||||
}
|
||||
|
||||
return remainder;
|
||||
}
|
||||
return remainder;
|
||||
}
|
||||
|
||||
[Params(7UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 63UL)]
|
||||
public ulong Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
/// <summary>
|
||||
/// `% 7` baseline; 0.274 ns at value 7, 0.270 ns at 8191, 0.268 ns at 131071, 0.270 ns at 2147483647, and 0.275 ns near ulong.Max.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return Value % 7UL;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension helper; 0.433 ns at value 7, 0.443 ns at 8191, 0.436 ns at 131071, 0.438 ns at 2147483647, but 0.684 ns near
|
||||
/// ulong.Max—about 1.6-2.5x slower overall.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Mod7(Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,19 +11,23 @@ public class Mod8UInt128Benchmarks
|
||||
[Params(8UL, 8191UL, 131071UL, 2147483647UL, ulong.MaxValue - 255UL)]
|
||||
public UInt128 Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// `% 8` baseline; 2.86-2.89 ns across the values (2.864 ns at value 8, 2.878 ns at 8191, 2.895 ns at 131071, 2.890 ns at
|
||||
/// 2147483647, 2.894 ns near ulong.Max).
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public ulong ModuloOperator()
|
||||
{
|
||||
return (ulong)(Value % 8UL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
/// <summary>
|
||||
/// Bitmask helper; reduced the cost to 0.010-0.020 ns (0.0170 ns at value 8, 0.0144 ns at 8191, 0.0134 ns at 131071, 0.0105 ns at
|
||||
/// 2147483647, 0.0203 ns near ulong.Max), delivering a 140-280x speedup.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public ulong ExtensionMethod()
|
||||
{
|
||||
return Value.Mod8();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,19 +11,23 @@ public class Mod8UIntBenchmarks
|
||||
[Params(8U, 2047U, 65535U, 2147483647U)]
|
||||
public uint Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// `% 8` baseline; means stayed around 0.011-0.015 ns (0.0109 ns at value 8, 0.0151 ns at 2047, 0.0123 ns at 65535, 0.0122 ns at
|
||||
/// 2147483647), i.e. indistinguishable from the empty-loop costs.
|
||||
/// </summary>
|
||||
[Benchmark(Baseline = true)]
|
||||
public uint ModuloOperator()
|
||||
{
|
||||
return Value % 8U;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fastest
|
||||
/// </summary>
|
||||
/// <summary>
|
||||
/// Bitmask helper (`value & 7U`); returned 0.0032 ns at value 8, 0.0113 ns at 2047, 0.0191 ns at 65535, and 0.0150 ns at
|
||||
/// 2147483647—roughly matching the baseline within measurement noise.
|
||||
/// </summary>
|
||||
[Benchmark]
|
||||
public uint ExtensionMethod()
|
||||
{
|
||||
return Value.Mod8();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user