rollup merge of #22151: Gankro/macro-bench

r? @alexcrichton
This commit is contained in:
Alex Crichton 2015-02-11 14:02:08 -08:00
commit fcf679cb88
4 changed files with 112 additions and 215 deletions

View File

@ -8,103 +8,113 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use prelude::*; macro_rules! map_insert_rand_bench {
use std::rand; ($name: ident, $n: expr, $map: ident) => (
use std::rand::Rng; #[bench]
use test::{Bencher, black_box}; pub fn $name(b: &mut ::test::Bencher) {
use std::rand;
use std::rand::Rng;
use test::black_box;
pub fn insert_rand_n<M, I, R>(n: usize, let n: usize = $n;
map: &mut M, let mut map = $map::new();
b: &mut Bencher, // setup
mut insert: I, let mut rng = rand::weak_rng();
mut remove: R) where
I: FnMut(&mut M, usize),
R: FnMut(&mut M, usize),
{
// setup
let mut rng = rand::weak_rng();
for _ in 0..n { for _ in 0..n {
insert(map, rng.gen::<usize>() % n); let i = rng.gen() % n;
} map.insert(i, i);
}
// measure // measure
b.iter(|| { b.iter(|| {
let k = rng.gen::<usize>() % n; let k = rng.gen() % n;
insert(map, k); map.insert(k, k);
remove(map, k); map.remove(&k);
}); });
black_box(map); black_box(map);
}
)
} }
pub fn insert_seq_n<M, I, R>(n: usize, macro_rules! map_insert_seq_bench {
map: &mut M, ($name: ident, $n: expr, $map: ident) => (
b: &mut Bencher, #[bench]
mut insert: I, pub fn $name(b: &mut ::test::Bencher) {
mut remove: R) where use test::black_box;
I: FnMut(&mut M, usize),
R: FnMut(&mut M, usize),
{
// setup
for i in 0..n {
insert(map, i * 2);
}
// measure let mut map = $map::new();
let mut i = 1; let n: usize = $n;
b.iter(|| { // setup
insert(map, i); for i in 0..n {
remove(map, i); map.insert(i * 2, i * 2);
i = (i + 2) % n; }
});
black_box(map); // measure
let mut i = 1;
b.iter(|| {
map.insert(i, i);
map.remove(&i);
i = (i + 2) % n;
});
black_box(map);
}
)
} }
pub fn find_rand_n<M, T, I, F>(n: usize, macro_rules! map_find_rand_bench {
map: &mut M, ($name: ident, $n: expr, $map: ident) => (
b: &mut Bencher, #[bench]
mut insert: I, pub fn $name(b: &mut ::test::Bencher) {
mut find: F) where use std::rand;
I: FnMut(&mut M, usize), use std::rand::Rng;
F: FnMut(&M, usize) -> T, use test::black_box;
{
// setup
let mut rng = rand::weak_rng();
let mut keys: Vec<_> = (0..n).map(|_| rng.gen::<usize>() % n).collect();
for k in &keys { let mut map = $map::new();
insert(map, *k); let n: usize = $n;
}
rng.shuffle(&mut keys); // setup
let mut rng = rand::weak_rng();
let mut keys: Vec<_> = (0..n).map(|_| rng.gen() % n).collect();
// measure for &k in &keys {
let mut i = 0; map.insert(k, k);
b.iter(|| { }
let t = find(map, keys[i]);
i = (i + 1) % n; rng.shuffle(&mut keys);
black_box(t);
}) // measure
let mut i = 0;
b.iter(|| {
let t = map.get(&keys[i]);
i = (i + 1) % n;
black_box(t);
})
}
)
} }
pub fn find_seq_n<M, T, I, F>(n: usize, macro_rules! map_find_seq_bench {
map: &mut M, ($name: ident, $n: expr, $map: ident) => (
b: &mut Bencher, #[bench]
mut insert: I, pub fn $name(b: &mut ::test::Bencher) {
mut find: F) where use test::black_box;
I: FnMut(&mut M, usize),
F: FnMut(&M, usize) -> T,
{
// setup
for i in 0..n {
insert(map, i);
}
// measure let mut map = $map::new();
let mut i = 0; let n: usize = $n;
b.iter(|| {
let x = find(map, i); // setup
i = (i + 1) % n; for i in 0..n {
black_box(x); map.insert(i, i);
}) }
// measure
let mut i = 0;
b.iter(|| {
let x = map.get(&i);
i = (i + 1) % n;
black_box(x);
})
}
)
} }

View File

@ -1843,74 +1843,18 @@ mod bench {
use test::{Bencher, black_box}; use test::{Bencher, black_box};
use super::BTreeMap; use super::BTreeMap;
use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n};
#[bench] map_insert_rand_bench!{insert_rand_100, 100, BTreeMap}
pub fn insert_rand_100(b: &mut Bencher) { map_insert_rand_bench!{insert_rand_10_000, 10_000, BTreeMap}
let mut m = BTreeMap::new();
insert_rand_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
#[bench] map_insert_seq_bench!{insert_seq_100, 100, BTreeMap}
pub fn insert_rand_10_000(b: &mut Bencher) { map_insert_seq_bench!{insert_seq_10_000, 10_000, BTreeMap}
let mut m = BTreeMap::new();
insert_rand_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
// Insert seq map_find_rand_bench!{find_rand_100, 100, BTreeMap}
#[bench] map_find_rand_bench!{find_rand_10_000, 10_000, BTreeMap}
pub fn insert_seq_100(b: &mut Bencher) {
let mut m = BTreeMap::new();
insert_seq_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
#[bench] map_find_seq_bench!{find_seq_100, 100, BTreeMap}
pub fn insert_seq_10_000(b: &mut Bencher) { map_find_seq_bench!{find_seq_10_000, 10_000, BTreeMap}
let mut m = BTreeMap::new();
insert_seq_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
// Find rand
#[bench]
pub fn find_rand_100(b: &mut Bencher) {
let mut m = BTreeMap::new();
find_rand_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
#[bench]
pub fn find_rand_10_000(b: &mut Bencher) {
let mut m = BTreeMap::new();
find_rand_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
// Find seq
#[bench]
pub fn find_seq_100(b: &mut Bencher) {
let mut m = BTreeMap::new();
find_seq_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
#[bench]
pub fn find_seq_10_000(b: &mut Bencher) {
let mut m = BTreeMap::new();
find_seq_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
fn bench_iter(b: &mut Bencher, size: i32) { fn bench_iter(b: &mut Bencher, size: i32) {
let mut map = BTreeMap::<i32, i32>::new(); let mut map = BTreeMap::<i32, i32>::new();

View File

@ -102,7 +102,7 @@ pub mod btree_set {
} }
#[cfg(test)] mod bench; #[cfg(test)] #[macro_use] mod bench;
// FIXME(#14344) this shouldn't be necessary // FIXME(#14344) this shouldn't be necessary
#[doc(hidden)] #[doc(hidden)]

View File

@ -1321,74 +1321,17 @@ mod test_map {
#[cfg(test)] #[cfg(test)]
mod bench { mod bench {
use test::Bencher;
use super::VecMap; use super::VecMap;
use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n};
#[bench] map_insert_rand_bench!{insert_rand_100, 100, VecMap}
pub fn insert_rand_100(b: &mut Bencher) { map_insert_rand_bench!{insert_rand_10_000, 10_000, VecMap}
let mut m = VecMap::new();
insert_rand_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
#[bench] map_insert_seq_bench!{insert_seq_100, 100, VecMap}
pub fn insert_rand_10_000(b: &mut Bencher) { map_insert_seq_bench!{insert_seq_10_000, 10_000, VecMap}
let mut m = VecMap::new();
insert_rand_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
// Insert seq map_find_rand_bench!{find_rand_100, 100, VecMap}
#[bench] map_find_rand_bench!{find_rand_10_000, 10_000, VecMap}
pub fn insert_seq_100(b: &mut Bencher) {
let mut m = VecMap::new();
insert_seq_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
#[bench] map_find_seq_bench!{find_seq_100, 100, VecMap}
pub fn insert_seq_10_000(b: &mut Bencher) { map_find_seq_bench!{find_seq_10_000, 10_000, VecMap}
let mut m = VecMap::new();
insert_seq_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
// Find rand
#[bench]
pub fn find_rand_100(b: &mut Bencher) {
let mut m = VecMap::new();
find_rand_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
#[bench]
pub fn find_rand_10_000(b: &mut Bencher) {
let mut m = VecMap::new();
find_rand_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
// Find seq
#[bench]
pub fn find_seq_100(b: &mut Bencher) {
let mut m = VecMap::new();
find_seq_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
#[bench]
pub fn find_seq_10_000(b: &mut Bencher) {
let mut m = VecMap::new();
find_seq_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
} }