10 fn binary_search<F>(b: &mut Bencher, cache: Cache, mapper: F)
12 F: Fn(usize) -> usize,
14 let size = match cache {
15 Cache::L1 => 1000, // 8kb
16 Cache::L2 => 10_000, // 80kb
17 Cache::L3 => 1_000_000, // 8Mb
19 let v = (0..size).map(&mapper).collect::<Vec<_>>();
22 // LCG constants from https://en.wikipedia.org/wiki/Numerical_Recipes.
23 r = r.wrapping_mul(1664525).wrapping_add(1013904223);
24 // Lookup the whole range to get 50% hits and 50% misses.
25 let i = mapper(r % size);
26 black_box(v.binary_search(&i).is_ok());
31 fn binary_search_l1(b: &mut Bencher) {
32 binary_search(b, Cache::L1, |i| i * 2);
36 fn binary_search_l2(b: &mut Bencher) {
37 binary_search(b, Cache::L2, |i| i * 2);
41 fn binary_search_l3(b: &mut Bencher) {
42 binary_search(b, Cache::L3, |i| i * 2);
46 fn binary_search_l1_with_dups(b: &mut Bencher) {
47 binary_search(b, Cache::L1, |i| i / 16 * 16);
51 fn binary_search_l2_with_dups(b: &mut Bencher) {
52 binary_search(b, Cache::L2, |i| i / 16 * 16);
56 fn binary_search_l3_with_dups(b: &mut Bencher) {
57 binary_search(b, Cache::L3, |i| i / 16 * 16);
61 ($fn:ident, $n:expr, $mapper:expr) => {
63 fn $fn(b: &mut Bencher) {
64 let mut x = (0usize..$n).map(&$mapper).collect::<Vec<_>>();
67 x[..].rotate_right(s);
69 black_box(x[0].clone())
76 struct Rgb(u8, u8, u8);
78 rotate!(rotate_u8, 32, |i| i as u8);
79 rotate!(rotate_rgb, 32, |i| Rgb(i as u8, (i as u8).wrapping_add(7), (i as u8).wrapping_add(42)));
80 rotate!(rotate_usize, 32, |i| i);
81 rotate!(rotate_16_usize_4, 16, |i| [i; 4]);
82 rotate!(rotate_16_usize_5, 16, |i| [i; 5]);
83 rotate!(rotate_64_usize_4, 64, |i| [i; 4]);
84 rotate!(rotate_64_usize_5, 64, |i| [i; 5]);