Created
September 4, 2016 13:34
-
-
Save yberreby/70df45bdec53ca8455e9d581ad49d5f1 to your computer and use it in GitHub Desktop.
Rust Benchmark - printing to stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! 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