Created
September 21, 2016 22:40
-
-
Save matklad/87a77a7670b1b9144e09b9c4e62ea00f to your computer and use it in GitHub Desktop.
Rust CString drop benchmark
This file contains hidden or 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
extern crate time; | |
extern crate rand; | |
use time::precise_time_ns; | |
use rand::Rng; | |
use std::ffi::CString; | |
fn main() { | |
println!("Baseline 1: empty String"); | |
bench3::<String>(String::new()); | |
println!("Baseline 2: hello world String"); | |
bench3::<String>(String::from("Hello, World")); | |
println!("Bench 1: empty CString"); | |
bench3::<CString>(CString::new("").unwrap()); | |
println!("Bench 2: hello world CString"); | |
bench3::<CString>(CString::new("Hello, World").unwrap()); | |
} | |
fn bench3<T: Clone>(x: T) { | |
let t = bench_drop(&x); | |
println!("ns per iter = {}", t / 1_000_000); | |
let t = bench_drop(&x); | |
println!("ns per iter = {}", t / 1_000_000); | |
let t = bench_drop(&x); | |
println!("ns per iter = {}\n", t / 1_000_000); | |
} | |
fn bench_drop<T: Clone>(x: &T) -> u64 { | |
let mut xs: Vec<T> = vec![x.clone(); 1_000_000]; | |
rand::weak_rng().shuffle(&mut xs); | |
let start = precise_time_ns(); | |
for x in xs { | |
drop(x) | |
} | |
let end = precise_time_ns(); | |
end - start | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment