Skip to content

Instantly share code, notes, and snippets.

@crumblingstatue
Created November 7, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crumblingstatue/346ed0a66f68a3715f08 to your computer and use it in GitHub Desktop.
Save crumblingstatue/346ed0a66f68a3715f08 to your computer and use it in GitHub Desktop.
#![feature(test, clone_from_slice)]
extern "C" {
fn memcpy(dest: *mut libc::c_void, src: *const libc::c_void, n: libc::size_t) -> *mut libc::c_void;
}
extern crate test;
extern crate libc;
const TEXT: &'static [u8] =
b"The big brown fox jumps over the high fence or something.
I don't know.
How am I supposed to know how big the data has to be for uhh...
Optimal benchmark results? Probably this should be enough.
Or maybe not.
Maybe there is a big difference when we are copying a lot of data.
Gotta gather more data. More and more entropy.
Gotta make the bytes varied.
Like this:
o[idfjqw0]eodfm-w]9kdf9-2jkr9023j4]9023md23o-dk-23,d[23kd
23dpoj230d9329]=dmk2390dk23p[dpo23kdpo23kdpo2kdp[2kdpo32kdp23
23dj23=09dk023=9djk023=9jd023jd092j3dj23p[djpo23djpo2jdp[23dp
=== The End ===
";
#[bench]
fn bench_clone_from_slice(b: &mut test::Bencher) {
b.iter(||
unsafe {
let mut dest = Vec::with_capacity(TEXT.len());
dest.set_len(TEXT.len());
dest.clone_from_slice(TEXT);
test::black_box(dest);
}
);
}
#[bench]
fn bench_manual_loop(b: &mut test::Bencher) {
b.iter(||
unsafe {
let mut dest = Vec::with_capacity(TEXT.len());
dest.set_len(TEXT.len());
for (sb, db) in TEXT.iter().zip(dest.iter_mut()) {
*db = *sb;
}
test::black_box(dest);
}
);
}
#[bench]
fn bench_memcpy(b: &mut test::Bencher) {
b.iter(||
unsafe {
let mut dest = Vec::with_capacity(TEXT.len());
dest.set_len(TEXT.len());
memcpy(dest.as_mut_ptr() as *mut libc::c_void, TEXT.as_ptr() as *const libc::c_void, TEXT.len());
test::black_box(dest);
}
);
}
test bench_clone_from_slice ... bench: 79 ns/iter (+/- 1)
test bench_manual_loop ... bench: 104 ns/iter (+/- 1)
test bench_memcpy ... bench: 79 ns/iter (+/- 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment