Skip to content

Instantly share code, notes, and snippets.

@bluss
Created September 10, 2016 22:45
Show Gist options
  • Save bluss/3a0afb7345000ec76bb2baeadf10f921 to your computer and use it in GitHub Desktop.
Save bluss/3a0afb7345000ec76bb2baeadf10f921 to your computer and use it in GitHub Desktop.
Using rustc 1.13.0-nightly (923bac459 2016-09-06) x86-64/linux
#
test clone_str ... bench: 370,601 ns/iter (+/- 14,692) = 2829 MB/s
test clone_vec_from_fn ... bench: 372,835 ns/iter (+/- 38,525) = 2812 MB/s
test clone_vec_from_incremental ... bench: 369,055 ns/iter (+/- 8,690) = 2841 MB/s
test fast_clone_vec ... bench: 374,947 ns/iter (+/- 11,168) = 2796 MB/s
#![feature(test)]
extern crate test;
use test::{Bencher, black_box};
static SIZE: usize = 1024*1024;
#[bench]
fn clone_str(bh: &mut Bencher) {
let mut x = String::with_capacity(SIZE);
for _ in 0..SIZE {
x.push('x');
}
bh.iter(|| x.clone());
bh.bytes = x.len() as u64;
}
#[bench]
fn clone_vec_from_incremental(bh: &mut Bencher) {
let mut x: Vec<u8> = Vec::with_capacity(SIZE);
for _ in 0..SIZE {
x.push(0x78);
}
bh.iter(|| x.clone());
bh.bytes = x.len() as u64;
}
#[bench]
fn clone_vec_from_fn(bh: &mut Bencher) {
let x = black_box(vec![0x78u8; SIZE]);
bh.iter(|| x.clone());
bh.bytes = x.len() as u64;
}
#[bench]
fn fast_clone_vec(bh: &mut Bencher) {
let x = black_box(vec![0x78u8; SIZE]);
bh.iter(|| {
let mut copy = Vec::<u8>::with_capacity(SIZE);
unsafe {
std::ptr::copy_nonoverlapping(x.as_ptr(), copy.as_mut_ptr(), x.len());
copy.set_len(SIZE);
}
copy
});
bh.bytes = x.len() as u64;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment