5 ms·
> That's going to be slower than a similar simple FizzBuzz that just goes to 2^54-1, but I don't think it would be a lot slower. OK, I've actually tried it, af
by tzs 4y ago
> That's going to be slower than a similar simple FizzBuzz that just goes to 2^54-1, but I don't think it would be a lot slower.
OK, I've actually tried it, after fixing the issue Allypost identified with the original code (JavaScript integer arithmetic only works up to 2^53, not the 2^54-1 I had assumed).
Here's a simple FizzBuzz:
let n = 1
for (let r = 1; r < 9990000; ++r) {
if (n == 0) {
console.log('FizzBuzz')
} else if (n % 3 == 0) {
console.log('Fizz')
} else if (n % 5 == 0) {
console.log('Buzz')
} else {
console.log(r)
}
if (++n == 15) n = 0
}
I compared that to the the more complicated one, with the outer loop changed to just go to 999, so it would produce the same output as the simple FizzBuzz. (And with the more complicated one changed to use console.log for output instead of the dummy functions in the original, itos() replaces by letting JavaScript implicitly do the conversion, and getting rid of explicit newlines since console.log works in lines.
Running each a few times via node with stdout directed to /dev/null, the simpler might be slightly faster but there is enough variation run to run that there is overlap. E.g., I got 23.811 vs 23.979 when I ran the two once, and then 24.325 and 25.550 when I ran them again.
Similar relative results when directing output to a file. There I actually saw in most runs the complex one run slightly faster than the simple one, but there was enough variation that I can't really say that one was on average faster than the other. The runs saving to disk took about 2.3x as long as the ones that discarded output.
None of them were anywhere near the speed of the programs in other languages on the code golf page linked a fews comments upthread. We're looking at about 5.4 MB/second when not saving the output to disk. That's around 1/20th the speed of the slowest on on the code golf page.
I'm just running things with "node file.js". I don't know if there are options that could be set to make it faster.
Anyway, the assertion upthread that JavaScript is not fast for this seems plausible, though not for the reason given (that it would need BigInt).
- kaba0 4y agoIt depends more on the particular IO-behavior than the language. (How often does it flush for example)