Re:BigInt Shipping in Firefox
by umanwizard on Thursday May 23, 2019 @08:06PM
(#19997252)
25 nanoseconds is much longer than a normal double-precision addition, loop counter increment, and conditional jump back to the top of a loop should take, so there's something other than the time taken by additions that's going on in your benchmark. I'm not a Node.JS expert, but I suspect it's not getting JITted properly, or getting poorly optimized if so.
I tried in C:
#include <math.h>
#include <stdio.h>
int main()
{
double a = pow(2, 100);
for (double i = 0; i < 1e7; ++i) {
a += i;
}
printf("%f\n", a);
}
and timed it. The time taken was 17ms.Rearanged your js sample a bit, now it runs in ~26ms (first time) and ~11ms (subsequent times) instead of ~220ms in the chrome developer console.
{
let a = Math.pow(2, 1023)
let t = performance.now();
let max = 1e7;
for (let i = 0; i < max; i++) {
a += i;
}
console.log(performance.now() - t);
}
Main problem was, that you should declare a with let.That benchmark is a bit strange/flawed anyway. You're initializing a as pow(2, 1023), then adding numbers in the loop. But since a is already such a large double value, the result won't change. The numbers you add are too small to make a dent in the value of a, likely because a isn't an integer. It's a double with a limited precision for large integer values.
Lowering the exponent to 100, JS spends 269ms and pypy 142. Not sure why that is, but having arbitrarily large integers doens't seem to make this arithmetic any faster.
I don't know how to quickly toy around with fraction-based floats, but at least for arbitrarily large integers, I'm not sure why we're going to have to put up with new syntax.