I would guess it's the println! macro eating the time.
I would guess it's the println! macro eating the time.
In my machine the naive C implementation achieves 81.7 MiB/s, whereas a naive Rust implementation with println achieves 7.58 MiB/s.
When I acquired the stdout's lock once and use writeln!(&mut stdout, ...) instead of println, I got 8.15 MiB/s.
When I wrapped stdout with BufWriter, I got 299 MiB/s.
use std::io::{BufWriter, Write};
fn main() {
let stdout = std::io::stdout();
let stdout = stdout.lock();
let mut stdout = BufWriter::with_capacity(32 * 1024, stdout);
for i in 1..1_000_000_000 {
let _ = match (i % 3 == 0, i % 5 == 0) {
(true, true) => writeln!(&mut stdout, "FizzBuzz"),
(true, false) => writeln!(&mut stdout, "Fizz"),
(false, true) => writeln!(&mut stdout, "Buzz"),
(false, false) => writeln!(&mut stdout, "{}", i),
};
}
}Calling stdout.lock() by itself makes little difference for me. Using BufWriter as you have I get ~80 MiB/s (slightly ahead of PyPy).
My implementation also has/had a String allocation in the loop, kicking that out got me to 120 MiB/s. I tend to write it as though the numbers/strings could be changed by a product manager at any time.
Final edit/errata: I get up to 140 MiB/s by appending .to_string() in the integer case instead of format!(), which makes some sense.
I didn't bother saving my original C implementation, but in retrospect I think I didn't use string concatenation, as I have with other languages. Redoing it with string-concatenation, C is slightly slower than Rust, 110 MiBs. Also makes sense as concatenation in C is slow.
Ruby is slightly slower than Python or Rust, 11 MB/s. I was expecting Python to be slowest among the interpreted languages, apparently not. Pypy yields a speedup to 70 MB/s.
Summing naive implementations (for each i: start with empty string, append fizz/buzz based on the modulus of each, then conditionally append i and print):
Julia 45 KB/s Ruby 11 MB/s CPython 15 MB/s Rust 16 MB/s Pypy 70 MB/s C 160 MB/s
Measured over 10 s after a 10 s burn-in period, Julia 1.7 on Ubuntu 21.10.