Re:Node.js 14 is over 20x faster than Python3.8 for fib(n)
by SuchAnonMuchWow on Tuesday February 09, 2021 @01:07PM
(#26080655)
Doesn't the Node.js version use double precision floating point vs python using infinite precision integers ?
That would explain part of the difference in performance, and make the python version exact, but js version inexact.
[ Parent
| on HN ]
Re:Node.js 14 is over 20x faster than Python3.8 for fib(n)
by akubera on Tuesday February 09, 2021 @01:34PM
(#26080971)
I assumed since this seems to be testing function call overhead, rather than the math, that an equivalent function with BigIntegers would be about the same for JS. But I tried it just for fun:
const { performance } = require('perf_hooks');
function fib(n) {
if (n == 0 || n == 1) { return 1; }
return fib(n - 1) + fib(n - 2);
}
function fibn(n) {
if (n == 0n || n == 1n) { return 1n; }
return fibn(n - 1n) + fibn(n - 2n);
}
var t0 = performance.now(); fib(35); console.log("fib:", performance.now() - t0);
var t0 = performance.now(); fibn(35n); console.log("fibn:", performance.now() - t0);
results: fib: 127.30008998513222
fibn: 2134.1405459940434
yikes, I think you're right. (nodejs v15.6.0)(An equivalent version with Python on my machine: ~2657.3ms)
Re:Node.js 14 is over 20x faster than Python3.8 for fib(n)
by edflsafoiewq on Tuesday February 09, 2021 @01:35PM
(#26080980)
This looks like most of the difference.
JS with number 138 ms (x1)
JS with BigInt (eg. 35n) 2620 ms (x19)
Python 3260 ms (x24)
Re:Node.js 14 is over 20x faster than Python3.8 for fib(n)
by meetups323 on Tuesday February 09, 2021 @01:15PM
(#26080744)
This is correct, though both are exact at the scales in question (14930352). There's a more general question of whether it's beneficial to take a perf hit for all arithmetic in order to support some corner cases of infinite precision, or be explicit that standard arithmetic won't be precise, but you can opt into it via some mechanism (BigNum, in the JS case)
I prefer the JS approach.
Re:Node.js 14 is over 20x faster than Python3.8 for fib(n)
by wongarsu on Tuesday February 09, 2021 @01:16PM
(#26080754)
The specifications do say that JavaScript only has floating point numbers (and no integers) and that Python has infinite precision integers. But in implementation, for whole numbers small enough to fit into a regular 32-bit integer both Python and Node.js use regular integers as an optimization. fib(35) comfortably fits that.
I guess node.js might have the edge here with the necessary overflow checks in the addition. Both languages have to do them to fall back to either doubles or bigints respectively, but Node.js's JIT can probably do them faster.
Re:Node.js 14 is over 20x faster than Python3.8 for fib(n)
by d0mine on Tuesday February 09, 2021 @01:12PM
(#26080716)
The numbers are small so there shouldn’t be any difference for fib(35)
Re:Node.js 14 is over 20x faster than Python3.8 for fib(n)
by ralusek on Tuesday February 09, 2021 @09:27PM
(#26085132)
They're fundamentally stored as and operated on in different ways. A BigInt does suddenly become a BigInt when in excess of MAX_SAFE_INTEGER, it's a BigInt even with a value of 1n.
Re:Node.js 14 is over 20x faster than Python3.8 for fib(n)
by d0mine on Friday February 12, 2021 @12:08PM
(#26115838)
fib(35) is much much smaller than the safe max that is the whole point of my comment
Re:Node.js 14 is over 20x faster than Python3.8 for fib(n)
by nullcat on Tuesday February 09, 2021 @01:11PM
(#26080702)
That's one factor but there is an overall reason behind performance difference. The main reason is v8 and the inordinate amount of resources google has thrown against that thing to make it ultra fast. You should read up on the people who work on it.