Skip to content

Instantly share code, notes, and snippets.

@bluss
Created November 23, 2016 23:12
Show Gist options
  • Save bluss/3fed8a20667d525a5f8a4713442fe540 to your computer and use it in GitHub Desktop.
Save bluss/3fed8a20667d525a5f8a4713442fe540 to your computer and use it in GitHub Desktop.
#![feature(test)]
extern crate test;
use std::mem::size_of_val;
use test::Bencher;
#[bench]
fn iter_all(bench: &mut Bencher) {
let mut b = vec![0u8; 64 * 1024];
const OFF: usize = 32 * 1024;
b[OFF] = 1;
bench.iter(|| {
b.iter().all(|x| *x == 0)
});
bench.bytes = OFF as u64 * size_of_val(&b[0]) as u64;
}
#[bench]
fn iter_find(bench: &mut Bencher) {
let mut b = vec![0u8; 64 * 1024];
const OFF: usize = 32 * 1024;
b[OFF] = 1;
bench.iter(|| {
b.iter().find(|x| **x == 1)
});
bench.bytes = OFF as u64 * size_of_val(&b[0]) as u64;
}
#[bench]
fn iter_position(bench: &mut Bencher) {
let mut b = vec![0u8; 64 * 1024];
const OFF: usize = 32 * 1024;
b[OFF] = 1;
bench.iter(|| {
b.iter().position(|x| *x == 1)
});
bench.bytes = OFF as u64 * size_of_val(&b[0]) as u64;
}
const FIND_SKIP: &'static [usize] = &[3, 9, 4, 16, 3, 2, 1];
// The loop bench wants to test a find/scan loop where there are many
// short intervals
#[bench]
fn iter_find_split_loop_u8(bench: &mut Bencher) {
let mut b = vec![0u8; 512];
for i in FIND_SKIP.iter().cloned().cycle().scan(0, |st, x| { *st += x; Some(*st) }) {
if i >= b.len() { break; }
b[i] = 1;
}
bench.iter(|| {
let mut nfind = 0;
let mut slc = b.iter();
loop {
if let Some(_) = slc.find(|x| **x == 1) {
nfind += 1;
} else {
break;
}
}
nfind
});
bench.bytes = size_of_val(&b[..]) as u64;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment