Skip to content

Instantly share code, notes, and snippets.

@swarnimarun
Last active November 28, 2022 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swarnimarun/620dd57d2bdd3441c0573172fd60997f to your computer and use it in GitHub Desktop.
Save swarnimarun/620dd57d2bdd3441c0573172fd60997f to your computer and use it in GitHub Desktop.
benchmark results for hello world, pls ensure that terminal itself is not the bottleneck, by redirecting output to a non-busy file
#!/bin/sh
# Results
#
# Macbook M1 Pro 16GB RAM
# Chip: M1 Pro
#
# best of 5: 154 nsec per loop (+/- 10)
#
# lines printed: `wc -l`
# :: 13888889 (yep not so slow are we now haha!)
python3 -m timeit "print('Hello, World!')"
#![feature(test)]
extern crate test;
use test::Bencher;
#[bench]
/// [[benchmark-results]]
///
/// Macbook M1 Pro 16GB RAM
/// Chip: M1 Pro
///
/// 1,342 ns/iter (+/- 150)
///
/// lines printed: `wc -l`
/// :: 288007 (+/- 50000)
fn hello_world(b: &mut Bencher) {
b.iter(move || {
// With println! bench and test run without output capture
// this inflates the result of benchmarking
// ensure the use of, `--nocapture`
// ```sh
// cargo +nightly bench -Z unstable-options -- --nocapture
// ```
println!("Hello, World!");
});
}
#[bench]
/// [[benchmark-results]]
///
/// Macbook M1 Pro 16GB RAM
/// Chip: M1 Pro
///
/// 13 ns/iter (+/- 0)
///
/// lines printed: `wc -l`
/// :: 460836007 (does it even matter? it's a 3+ GB file)
fn better_hello_world(b: &mut Bencher) {
use std::io::Write;
let stdout = std::io::stdout();
let lock = stdout.lock();
let mut out = std::io::BufWriter::new(lock);
b.iter(|| {
test::black_box(write!(out, "Hello, World!\n"));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment