You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have, as a test of various big number modules, been calculating the 4784969th number in the Finonacci sequence. This is the first Fibonacci number of one million decimal digits. See code below. Whilst node-bignum seems to be a lot faster than say big-integer by a factor of about 20 for fibo(40000) it does use a lot of memory. fibo(400000) uses half of my 4GB RAM already! For comparison big-integer only uses 3% of my memory when calculating fibo(4784969).
I am doing something wrong, is this normal or a bug?
Edit: Using node 0.11.14 here.
Edit: Same problem with 0.10.32
var bigNumber = require("bignum");
function fibo_iterative(n) {
var first = bigNumber(0),
second = bigNumber(1),
next = bigNumber(0),
c;
if (n <= 1) {
return bigNumber(n);
}
for (c = 0; c < n - 1; c += 1) {
next = first.add(second);
first = second;
second = next;
}
return next;
}
var resultString;
var n = 4784969; // The first fibo number with one million digits!
resultString = fibo_iterative(n).toString();
console.log("The " + resultString.length + " digits of fibo(" + n + ") are:");
console.log(resultString);
console.log("Done.");
The text was updated successfully, but these errors were encountered:
Unfortunately I don't have a good answer for you here ; it could be a GC problem with the large loop, perhaps if it was broken up across event loop turns you might have different results, unfortunately that's not a great answer because it would significantly complicate your code.
I'll leave this issue open in the hope that someone else can answer it--I'm more of a maintainer rather than an expert user unfortunately!
I tried your suggestion and iterated the loop via a setInterval(), also tried a 1ms setTimeout().
This reduces my CPU load down a few percent and I guess it would take a little over an hour to complete.
Sadly it still ramps up to 30% of my 3GB RAM here after a few minutes.
Here is the code:
var bigNumber = require("bignum");
function fibo_iterative(n, callback) {
var first = bigNumber(0),
second = bigNumber(1),
next = bigNumber(0),
loop,
c;
if (n <= 1) {
callback (bigNumber(n));
} else {
c = 0;
(function loop () {
if (c < n - 1) {
next = first.add(second);
first = second;
second = next;
c += 1;
//setImmediate(loop);
setTimeout(loop, 1);
} else {
callback (next);
}
}());
}
}
var n = 4784969; // The first fibo number with one million digits!
fibo_iterative(n, function (r) {
var rs = r.toString();
console.log("The " + rs.length + " digits of fibo(" + n + ") are:");
console.log(rs);
console.log("Done.");
});
node-bignum uses all the memory in my system.
I have, as a test of various big number modules, been calculating the 4784969th number in the Finonacci sequence. This is the first Fibonacci number of one million decimal digits. See code below. Whilst node-bignum seems to be a lot faster than say big-integer by a factor of about 20 for fibo(40000) it does use a lot of memory. fibo(400000) uses half of my 4GB RAM already! For comparison big-integer only uses 3% of my memory when calculating fibo(4784969).
I am doing something wrong, is this normal or a bug?
Edit: Using node 0.11.14 here.
Edit: Same problem with 0.10.32
The text was updated successfully, but these errors were encountered: