Repository navigation
Closed
Description
progress: ^2.0.0
node: 8.9.1
OS: Windows 10
terminal: git bash 2.15.1
This is an algorithm for today's advent of code. I thought it would be fun to add a progress bar since it's quite long to run (it has to iterate 40 mil times).
But the progress bar just won't work. It works on a very small set or any of your examples, but not with the real data. Below code is in typescript:
import * as ProgressBar from 'progress';
const getNextValue = (n: number, generator: 'A' | 'B'): number => {
const factor = generator === 'A' ? 16807 : 48271;
return n * factor % 2147483647;
};
const getTotalMatches = (rounds = 40000000, generatorA = 512, generatorB = 191): number => {
const bar = new ProgressBar('[:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
total: rounds,
width: 10,
});
let r = rounds;
let nextA = generatorA;
let nextB = generatorB;
let totalMatches = 0;
while (r-- > 0) {
nextA = getNextValue(nextA, 'A');
nextB = getNextValue(nextB, 'B');
const nextABin = nextA.toString(2).split('').reverse().join('');
const nextBBin = nextB.toString(2).split('').reverse().join('');
if (nextABin.startsWith(nextBBin.substr(0, 16))) {
++totalMatches;
}
bar.tick();
}
return totalMatches;
};
const downloadExample = () => {
const contentLength = 128 * 1024;
const bar = new ProgressBar('[:bar] :percent :etas', {
complete: '=' ,
incomplete: ' ',
total: contentLength,
width: 20,
});
(function next() {
if (contentLength) {
const chunk = Math.random() * 10 * 1024;
bar.tick(chunk);
if (!bar.complete) {
setTimeout(next, Math.random() * 1000);
}
}
})();
};
// console.log(`Test should be 1: ${getTotalMatches(5, 65, 8921)}`); // ProgressBar displayed
// console.log(`Test should be 588: ${getTotalMatches(40000000, 65, 8921)}`); // ProgressBar not displayed
// console.log(`Result: ${getTotalMatches()}`); // ProgressBar not displayed
// console.log(downloadExample()); // ProgressBar displayed
I tried creating the ProgressBar with some ratio by basically doing total: rounds > 10 ? 10 : rounds, and surrounding the bar.tick() in if (rounds < 10 || r % (rounds / 10) === 0), but it doesn't work either.
Any cue? I might be blind or something...
Metadata
Metadata
Assignees
Labels
No labels
Activity
westwood846 commentedon Jan 8, 2018
I also had problems with the progress bar not rendering so I downgraded to 1.1.8 (
npm install --save [email protected]) and it's working. Didn't even have to change my code...westwood846 commentedon Mar 22, 2018
I managed to nail the problem down to synchronous iteration:
In the example above, the bar is never drawn. When using some high value for
leninstead ofInfinity, the bar eventually shows up, after the iteration is done.@thebigredgeek if I submit a pull request, will you merge it?
Add a synchronous example
Remove the render throttle logic
Don't render if last render was less than renderThrottle ms ago
westwood846 commentedon Mar 22, 2018
In any case, there you go. Would be great if you could merge this. Let me know if the PR needs any additional changes.