Last active
November 1, 2020 06:05
-
-
Save Kogia-sima/167ec68c68829fe377be96f56af077a8 to your computer and use it in GitHub Desktop.
Vec::drain benchmark
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
#![feature(test)] | |
extern crate test; | |
use test::{Bencher, black_box}; | |
type Item = u8; | |
const TOTAL_SIZE: usize = 10000; | |
const COPY_SIZE: usize = 1000; | |
#[bench] | |
fn vec_drain(b: &mut Bencher) { | |
let mut v = black_box(vec![42; TOTAL_SIZE]); | |
b.iter(|| { | |
unsafe { | |
v.set_len(TOTAL_SIZE); | |
v.drain(..TOTAL_SIZE - COPY_SIZE); | |
}; | |
}); | |
} | |
#[bench] | |
fn ptr_copy(b: &mut Bencher) { | |
let mut v = black_box(vec![42; TOTAL_SIZE]); | |
b.iter(|| { | |
unsafe { | |
v.set_len(TOTAL_SIZE); | |
std::ptr::drop_in_place(std::ptr::slice_from_raw_parts_mut(v.as_mut_ptr(), TOTAL_SIZE - COPY_SIZE)); | |
let src = v.as_ptr().add(TOTAL_SIZE - COPY_SIZE); | |
let dst = v.as_mut_ptr(); | |
std::ptr::copy(src, dst, COPY_SIZE); | |
v.set_len(COPY_SIZE); | |
}; | |
}); | |
} |
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
$ cargo +nightly bench | |
Compiling vec-drain-issue v0.1.0 (/home/kogia-sima/PG/Rust/vec-drain-issue) | |
Finished bench [optimized] target(s) in 1.60s | |
Running target/release/deps/vec_drain_issue-e603aeb59eacfab3 | |
running 0 tests | |
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out | |
Running target/release/deps/bench-66a9fcfcff8f19bc | |
running 3 tests | |
test ptr_copy ... bench: 31 ns/iter (+/- 0) | |
test vec_drain ... bench: 5710 ns/iter (+/- 0) | |
test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured; 0 filtered out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment