Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//! rustc -C opt-level=3 --test cowbell.rs && ./cowbell --bench
//!
//! running 8 tests
//! test empty ... bench: 0 ns/iter (+/- 0)
//! test format_str ... bench: 70 ns/iter (+/- 2)
//! test format_str_cow ... bench: 76 ns/iter (+/- 0)
//! test format_string ... bench: 75 ns/iter (+/- 0)
//! test format_string_cow ... bench: 78 ns/iter (+/- 1)
//! test to_cow ... bench: 2 ns/iter (+/- 0)
//! test to_owned_cow ... bench: 27 ns/iter (+/- 0)
//! test to_owned_string ... bench: 33 ns/iter (+/- 0)
//!
//! test result: ok. 0 passed; 0 failed; 0 ignored; 8 measured
#![feature(test)]
extern crate test;
use test::Bencher;
use std::borrow::{Cow, ToOwned};
#[bench]
fn empty(b: &mut Bencher) {
b.iter(|| 1)
}
#[bench]
fn to_owned_string(b: &mut Bencher) {
b.iter(|| "Benchmark".to_owned());
}
#[bench]
fn to_cow(b: &mut Bencher) {
let s: &'static str = "Benchmark";
b.iter(|| Cow::Borrowed(s));
}
#[bench]
fn to_owned_cow(b: &mut Bencher) {
let s : String = "Benchmark".to_owned();
b.iter(|| { let c : Cow<'static, str> = Cow::Owned(s.clone()); c });
}
#[bench]
fn format_string(b: &mut Bencher) {
let s : String = "Benchmark".to_owned();
b.iter(|| format!("{}", &s));
}
#[bench]
fn format_str(b: &mut Bencher) {
let s : &'static str = "Benchmark";
b.iter(|| format!("{}", s));
}
#[bench]
fn format_string_cow(b: &mut Bencher) {
let c : Cow<'static, str> = Cow::Owned("Benchmark".to_owned());
b.iter(|| format!("{}", &c));
}
#[bench]
fn format_str_cow(b: &mut Bencher) {
let s : Cow<'static, str> = Cow::Borrowed("Benchmark");
b.iter(|| format!("{}", &s));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment