Skip to content

Instantly share code, notes, and snippets.

@yberreby
Created September 4, 2016 13:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yberreby/70df45bdec53ca8455e9d581ad49d5f1 to your computer and use it in GitHub Desktop.
Save yberreby/70df45bdec53ca8455e9d581ad49d5f1 to your computer and use it in GitHub Desktop.
Rust Benchmark - printing to stdout
print_macro: 105 ns/iter (+/- 20)
print_macro_locked_stdoutbench: 87 ns/iter (+/- 22)
direct_locked_stdout: 17 ns/iter (+/- 2)
direct_unlocked_stdout: 51 ns/iter (+/- 9)
//! WARNING: running this benchmark will output a huge amount of data to stdout.
#![feature(test)]
#![feature(str_char)]
extern crate test;
extern crate libc;
use test::Bencher;
use std::io;
use std::io::Write;
const TEST_STR: &'static str = "hello";
#[bench]
fn print_macro(b: &mut Bencher) {
b.iter(|| {
print!("{}", TEST_STR);
});
print!("print_macro");
}
#[bench]
fn print_macro_locked_stdout(b: &mut Bencher) {
let mut stdout = std::io::stdout();
let stdout = stdout.lock();
b.iter(|| {
print!("{}", TEST_STR);
});
print!("print_macro_locked_stdout");
}
#[bench]
fn direct_unlocked_stdout(b: &mut Bencher) {
let mut stdout = io::stdout();
b.iter(|| {
stdout.write(TEST_STR.as_bytes());
});
print!("direct_unlocked_stdout");
}
#[bench]
fn direct_locked_stdout(b: &mut Bencher) {
let mut stdout = io::stdout();
let mut stdout = stdout.lock();
b.iter(|| {
stdout.write(TEST_STR.as_bytes());
});
print!("direct_locked_stdout");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment