|
| 1 | +// Copyright (c) 2023-2026 ktsu-dev contributors |
| 2 | + |
| 3 | +namespace ktsu.PreciseNumber.Benchmarks; |
| 4 | + |
| 5 | +using System.Globalization; |
| 6 | + |
| 7 | +using BenchmarkDotNet.Attributes; |
| 8 | +using BenchmarkDotNet.Configs; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Measures what this type costs against the same arithmetic on a bare <see cref="double"/>. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// <para> |
| 15 | +/// Every other class here answers "how long does this operation take", which is only readable next |
| 16 | +/// to something. This one supplies the something: the primitive a caller would otherwise have |
| 17 | +/// used. The same class, with the same loops and the same methodology, is in ktsu.SignificantNumber |
| 18 | +/// and ktsu.Semantics, so the three answers are comparable with each other as well as with |
| 19 | +/// <see cref="double"/>. |
| 20 | +/// </para> |
| 21 | +/// <para> |
| 22 | +/// The bare method is the BenchmarkDotNet baseline, so the answer is the <c>Ratio</c> column rather |
| 23 | +/// than two rows divided by hand. Unlike the wrapper types built on this one, a ratio here is not |
| 24 | +/// expected to be 1.00 and is not a defect when it is not: arbitrary precision is a cost paid for |
| 25 | +/// something a <see cref="double"/> cannot do at all. What the number is for is watching that cost |
| 26 | +/// across releases. |
| 27 | +/// </para> |
| 28 | +/// <para> |
| 29 | +/// <b>Why these are loops.</b> A single operation over operands that do not change is |
| 30 | +/// loop-invariant, and the JIT hoists it out of the measurement entirely — for |
| 31 | +/// <see cref="double"/> that leaves a method indistinguishable from an empty one, and a ratio |
| 32 | +/// against an empty method means nothing. Here each iteration feeds the next, so there is nothing |
| 33 | +/// to hoist and both sides are measurable. |
| 34 | +/// </para> |
| 35 | +/// <para> |
| 36 | +/// <b>Which way the loop biases the answer.</b> Both sides pay the same counter and branch, and it |
| 37 | +/// is a dependency chain, so most of that overlaps the arithmetic; whatever does not is added |
| 38 | +/// equally to numerator and denominator and pulls the ratio toward 1.00. A ratio here is therefore |
| 39 | +/// a floor on the real cost rather than the whole of it. |
| 40 | +/// </para> |
| 41 | +/// <para> |
| 42 | +/// <b>Why the operands stay bounded, and why they are short.</b> This type carries as many digits |
| 43 | +/// as the arithmetic produces, so a chain that compounded its operand would measure that growth |
| 44 | +/// rather than the operation; both loops accumulate instead. The operands are also chosen to be |
| 45 | +/// values a <see cref="double"/> can hold, so the two sides are doing the same arithmetic on the |
| 46 | +/// same numbers rather than being handed different problems. How the cost grows with digits is a |
| 47 | +/// different question, and <see cref="ArithmeticBenchmarks"/> answers it across its |
| 48 | +/// <c>Digits</c> axis. |
| 49 | +/// </para> |
| 50 | +/// </remarks> |
| 51 | +[MemoryDiagnoser] |
| 52 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 53 | +[CategoriesColumn] |
| 54 | +public class AbstractionCostBenchmarks |
| 55 | +{ |
| 56 | + /// <summary> |
| 57 | + /// Operations per invocation. Enough that the loop's own cost is a small share of the work, |
| 58 | + /// few enough that the arbitrary-precision side still finishes an iteration promptly. |
| 59 | + /// </summary> |
| 60 | + private const int Operations = 256; |
| 61 | + |
| 62 | + private const string SeedText = "1234.5678901234"; |
| 63 | + private const string StepText = "0.0009765625"; |
| 64 | + private const string OtherText = "3.14159265358979"; |
| 65 | + |
| 66 | + private double bareSeed; |
| 67 | + private double bareStep; |
| 68 | + private double bareOther; |
| 69 | + |
| 70 | + // Assigned in GlobalSetup before anything is measured. Initialised here because this type |
| 71 | + // was a class before 2.0, where an unassigned field is a null reference the compiler |
| 72 | + // rejects; from 2.0 it is a struct and this is simply its default. The backfill measures |
| 73 | + // those releases too, so the file has to compile against both shapes. |
| 74 | + private PreciseNumber preciseSeed = default!; |
| 75 | + private PreciseNumber preciseStep = default!; |
| 76 | + private PreciseNumber preciseOther = default!; |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Prepares the operands, parsed from the same text on both sides. |
| 80 | + /// </summary> |
| 81 | + [GlobalSetup] |
| 82 | + public void Setup() |
| 83 | + { |
| 84 | + bareSeed = double.Parse(SeedText, CultureInfo.InvariantCulture); |
| 85 | + bareStep = double.Parse(StepText, CultureInfo.InvariantCulture); |
| 86 | + bareOther = double.Parse(OtherText, CultureInfo.InvariantCulture); |
| 87 | + |
| 88 | + preciseSeed = PreciseNumber.Parse(SeedText, CultureInfo.InvariantCulture); |
| 89 | + preciseStep = PreciseNumber.Parse(StepText, CultureInfo.InvariantCulture); |
| 90 | + preciseOther = PreciseNumber.Parse(OtherText, CultureInfo.InvariantCulture); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary>Adds along a chain, on a bare double.</summary> |
| 94 | + /// <returns>The accumulated value.</returns> |
| 95 | + [BenchmarkCategory("Add")] |
| 96 | + [Benchmark(Baseline = true, OperationsPerInvoke = Operations)] |
| 97 | + public double BareAdd() |
| 98 | + { |
| 99 | + double accumulator = bareSeed; |
| 100 | + |
| 101 | + for (int i = 0; i < Operations; i++) |
| 102 | + { |
| 103 | + accumulator += bareStep; |
| 104 | + } |
| 105 | + |
| 106 | + return accumulator; |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary>Adds along the same chain, on this type.</summary> |
| 110 | + /// <returns>The accumulated value.</returns> |
| 111 | + [BenchmarkCategory("Add")] |
| 112 | + [Benchmark(OperationsPerInvoke = Operations)] |
| 113 | + public PreciseNumber PreciseAdd() |
| 114 | + { |
| 115 | + PreciseNumber accumulator = preciseSeed; |
| 116 | + |
| 117 | + for (int i = 0; i < Operations; i++) |
| 118 | + { |
| 119 | + accumulator += preciseStep; |
| 120 | + } |
| 121 | + |
| 122 | + return accumulator; |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary>Multiplies and accumulates, on a bare double.</summary> |
| 126 | + /// <returns>The accumulated value.</returns> |
| 127 | + [BenchmarkCategory("Multiply")] |
| 128 | + [Benchmark(Baseline = true, OperationsPerInvoke = Operations)] |
| 129 | + public double BareMultiply() |
| 130 | + { |
| 131 | + double accumulator = 0d; |
| 132 | + double value = bareSeed; |
| 133 | + |
| 134 | + for (int i = 0; i < Operations; i++) |
| 135 | + { |
| 136 | + accumulator += value * bareOther; |
| 137 | + value += bareStep; |
| 138 | + } |
| 139 | + |
| 140 | + return accumulator; |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary>Multiplies and accumulates over the same values, on this type.</summary> |
| 144 | + /// <returns>The accumulated value.</returns> |
| 145 | + [BenchmarkCategory("Multiply")] |
| 146 | + [Benchmark(OperationsPerInvoke = Operations)] |
| 147 | + public PreciseNumber PreciseMultiply() |
| 148 | + { |
| 149 | + PreciseNumber accumulator = PreciseNumber.Zero; |
| 150 | + PreciseNumber value = preciseSeed; |
| 151 | + |
| 152 | + for (int i = 0; i < Operations; i++) |
| 153 | + { |
| 154 | + accumulator += value * preciseOther; |
| 155 | + value += preciseStep; |
| 156 | + } |
| 157 | + |
| 158 | + return accumulator; |
| 159 | + } |
| 160 | +} |
0 commit comments