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
// except according to those terms.
use prelude::*;
use std::rand;
use std::rand::Rng;
use test::{Bencher, black_box};
macro_rules! map_insert_rand_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
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,
map: &mut M,
b: &mut Bencher,
mut insert: I,
mut remove: R) where
I: FnMut(&mut M, usize),
R: FnMut(&mut M, usize),
{
// setup
let mut rng = rand::weak_rng();
let n: usize = $n;
let mut map = $map::new();
// setup
let mut rng = rand::weak_rng();
for _ in 0..n {
insert(map, rng.gen::<usize>() % n);
}
for _ in 0..n {
let i = rng.gen() % n;
map.insert(i, i);
}
// measure
b.iter(|| {
let k = rng.gen::<usize>() % n;
insert(map, k);
remove(map, k);
});
black_box(map);
// measure
b.iter(|| {
let k = rng.gen() % n;
map.insert(k, k);
map.remove(&k);
});
black_box(map);
}
)
}
pub fn insert_seq_n<M, I, R>(n: usize,
map: &mut M,
b: &mut Bencher,
mut insert: I,
mut remove: R) where
I: FnMut(&mut M, usize),
R: FnMut(&mut M, usize),
{
// setup
for i in 0..n {
insert(map, i * 2);
}
macro_rules! map_insert_seq_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use test::black_box;
// measure
let mut i = 1;
b.iter(|| {
insert(map, i);
remove(map, i);
i = (i + 2) % n;
});
black_box(map);
let mut map = $map::new();
let n: usize = $n;
// setup
for i in 0..n {
map.insert(i * 2, i * 2);
}
// 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,
map: &mut M,
b: &mut Bencher,
mut insert: I,
mut find: F) where
I: FnMut(&mut M, usize),
F: FnMut(&M, usize) -> T,
{
// setup
let mut rng = rand::weak_rng();
let mut keys: Vec<_> = (0..n).map(|_| rng.gen::<usize>() % n).collect();
macro_rules! map_find_rand_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use std::rand;
use std::rand::Rng;
use test::black_box;
for k in &keys {
insert(map, *k);
}
let mut map = $map::new();
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
let mut i = 0;
b.iter(|| {
let t = find(map, keys[i]);
i = (i + 1) % n;
black_box(t);
})
for &k in &keys {
map.insert(k, k);
}
rng.shuffle(&mut keys);
// 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,
map: &mut M,
b: &mut Bencher,
mut insert: I,
mut find: F) where
I: FnMut(&mut M, usize),
F: FnMut(&M, usize) -> T,
{
// setup
for i in 0..n {
insert(map, i);
}
macro_rules! map_find_seq_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use test::black_box;
// measure
let mut i = 0;
b.iter(|| {
let x = find(map, i);
i = (i + 1) % n;
black_box(x);
})
let mut map = $map::new();
let n: usize = $n;
// setup
for i in 0..n {
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 super::BTreeMap;
use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n};
#[bench]
pub fn insert_rand_100(b: &mut Bencher) {
let mut m = BTreeMap::new();
insert_rand_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
map_insert_rand_bench!{insert_rand_100, 100, BTreeMap}
map_insert_rand_bench!{insert_rand_10_000, 10_000, BTreeMap}
#[bench]
pub fn insert_rand_10_000(b: &mut Bencher) {
let mut m = BTreeMap::new();
insert_rand_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
map_insert_seq_bench!{insert_seq_100, 100, BTreeMap}
map_insert_seq_bench!{insert_seq_10_000, 10_000, BTreeMap}
// Insert seq
#[bench]
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); });
}
map_find_rand_bench!{find_rand_100, 100, BTreeMap}
map_find_rand_bench!{find_rand_10_000, 10_000, BTreeMap}
#[bench]
pub fn insert_seq_10_000(b: &mut Bencher) {
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); });
}
map_find_seq_bench!{find_seq_100, 100, BTreeMap}
map_find_seq_bench!{find_seq_10_000, 10_000, BTreeMap}
fn bench_iter(b: &mut Bencher, size: i32) {
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
#[doc(hidden)]

View File

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