Skip to content

Commit 1cc7ddc

Browse files
LiviaMedeirosaduh95
authored andcommitted
lib: replace Kahan summation with MathSumPrecise
Signed-off-by: LiviaMedeiros <livia@cirno.name> PR-URL: #67222 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8ed89aa commit 1cc7ddc

1 file changed

Lines changed: 3 additions & 13 deletions

File tree

‎lib/internal/bench_runner/benchmark.js‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ const {
1313
MathMin,
1414
MathRound,
1515
MathSqrt,
16+
MathSumPrecise,
1617
Number,
1718
NumberIsFinite,
1819
NumberMAX_SAFE_INTEGER,
1920
ObjectFreeze,
2021
ObjectKeys,
2122
PromiseWithResolvers,
23+
SafeArrayIterator,
2224
SafeSet,
2325
StringPrototypeToLowerCase,
2426
} = primordials;
@@ -485,18 +487,6 @@ class BenchContext {
485487
}
486488
}
487489

488-
function arithmeticMean(values) {
489-
let sum = 0;
490-
let compensation = 0;
491-
for (let i = 0; i < values.length; i++) {
492-
const adjusted = values[i] - compensation;
493-
const next = sum + adjusted;
494-
compensation = (next - sum) - adjusted;
495-
sum = next;
496-
}
497-
return sum / values.length;
498-
}
499-
500490
function summarizeSamples(samples) {
501491
const rates = [];
502492
let min = Infinity;
@@ -508,7 +498,7 @@ function summarizeSamples(samples) {
508498
max = MathMax(max, rate);
509499
}
510500

511-
const mean = arithmeticMean(rates);
501+
const mean = MathSumPrecise(new SafeArrayIterator(rates)) / rates.length;
512502
let variance = 0;
513503
for (let i = 0; i < rates.length; i++) {
514504
const difference = rates[i] - mean;

0 commit comments

Comments
 (0)