Skip to content

Commit

Permalink
Merge pull request #123 from Gankro/uhh
Browse files Browse the repository at this point in the history
replace old busted map benches with less obviously busted std bench macros
  • Loading branch information
apasel422 committed Feb 21, 2015
2 parents ca8f9ba + e3366ac commit 85859a1
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 158 deletions.
192 changes: 100 additions & 92 deletions src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,103 +8,111 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! map_insert_rand_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use rand::{self, Rng};
use test::black_box;

// some stuff is conditionally used by feature, so just allow this to be dead
#![allow(dead_code)]

use rand::{self, Rng};
use test::Bencher;

pub fn insert_rand_n<M, I, R>(n: u32,
map: &mut M,
b: &mut Bencher,
mut insert: I,
mut remove: R) where
I: FnMut(&mut M, u32),
R: FnMut(&mut M, u32),
{
// setup
let mut rng = rand::weak_rng();

for _ in range(0, n) {
insert(map, rng.gen::<u32>() % n);
}

// measure
b.iter(|| {
let k = rng.gen::<u32>() % n;
insert(map, k);
remove(map, k);
})
let n: usize = $n;
let mut map = $map::new();
// setup
let mut rng = rand::weak_rng();

for _ in 0..n {
let i = rng.gen() % n;
map.insert(i, i);
}

// 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: u32,
map: &mut M,
b: &mut Bencher,
mut insert: I,
mut remove: R) where
I: FnMut(&mut M, u32),
R: FnMut(&mut M, u32),
{
// setup
for i in range(0, n) {
insert(map, i * 2);
}

// measure
let mut i = 1;
b.iter(|| {
insert(map, i);
remove(map, i);
i = (i + 2) % n;
})
macro_rules! map_insert_seq_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use test::black_box;

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: u32,
map: &mut M,
b: &mut Bencher,
mut insert: I,
mut find: F) where
I: FnMut(&mut M, u32),
F: FnMut(&M, u32) -> T,
{
// setup
let mut rng = rand::weak_rng();
let mut keys: Vec<_> = range(0, n).map(|_| rng.gen::<u32>() % n).collect();

for k in keys.iter() {
insert(map, *k);
}

rng.shuffle(keys.as_mut_slice());

// measure
let mut i = 0;
b.iter(|| {
let t = find(map, keys[i as usize]);
i = (i + 1) % n;
t
})
macro_rules! map_find_rand_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use rand::{self, Rng};
use test::black_box;

let mut map = $map::new();
let n: usize = $n;

// setup
let mut rng = rand::weak_rng();
let mut keys: Vec<_> = (0..n).map(|_| rng.gen() % n).collect();

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: u32,
map: &mut M,
b: &mut Bencher,
mut insert: I,
mut find: F) where
I: FnMut(&mut M, u32),
F: FnMut(&M, u32) -> T,
{
// setup
for i in range(0, n) {
insert(map, i);
}

// measure
let mut i = 0;
b.iter(|| {
let x = find(map, i);
i = (i + 1) % n;
x
})
macro_rules! map_find_seq_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use test::black_box;

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);
})
}
)
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extern crate core;
#[cfg(feature="traverse")]
extern crate traverse;


#[cfg(test)] #[macro_use] mod bench;

// Re-Exports
#[cfg(feature="blist")] pub use blist::BList;
Expand All @@ -57,7 +57,6 @@ extern crate traverse;
// privates
#[cfg(feature="tree_map")] mod tree;
#[cfg(feature="trie_map")] mod trie;
#[cfg(test)] mod bench;

// publics

Expand Down
72 changes: 8 additions & 64 deletions src/tree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1920,74 +1920,18 @@ mod bench {
use test::{Bencher, black_box};

use super::TreeMap;
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: TreeMap<u32,u32> = TreeMap::new();
insert_rand_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}

#[bench]
pub fn insert_rand_10_000(b: &mut Bencher) {
let mut m: TreeMap<u32,u32> = TreeMap::new();
insert_rand_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}

// Insert seq
#[bench]
pub fn insert_seq_100(b: &mut Bencher) {
let mut m: TreeMap<u32,u32> = TreeMap::new();
insert_seq_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}

#[bench]
pub fn insert_seq_10_000(b: &mut Bencher) {
let mut m: TreeMap<u32,u32> = TreeMap::new();
insert_seq_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.remove(&i); });
}
map_insert_rand_bench!{insert_rand_100, 100, TreeMap}
map_insert_rand_bench!{insert_rand_10_000, 10_000, TreeMap}

// Find rand
#[bench]
pub fn find_rand_100(b: &mut Bencher) {
let mut m: TreeMap<u32,u32> = TreeMap::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: TreeMap<u32,u32> = TreeMap::new();
find_rand_n(10_000, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
map_insert_seq_bench!{insert_seq_100, 100, TreeMap}
map_insert_seq_bench!{insert_seq_10_000, 10_000, TreeMap}

// Find seq
#[bench]
pub fn find_seq_100(b: &mut Bencher) {
let mut m: TreeMap<u32,u32> = TreeMap::new();
find_seq_n(100, &mut m, b,
|m, i| { m.insert(i, 1); },
|m, i| { m.get(&i); });
}
map_find_rand_bench!{find_rand_100, 100, TreeMap}
map_find_rand_bench!{find_rand_10_000, 10_000, TreeMap}

#[bench]
pub fn find_seq_10_000(b: &mut Bencher) {
let mut m: TreeMap<u32,u32> = TreeMap::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, TreeMap}
map_find_seq_bench!{find_seq_10_000, 10_000, TreeMap}

fn bench_iter(b: &mut Bencher, size: usize) {
let mut map = TreeMap::<u32, u32>::new();
Expand Down
12 changes: 12 additions & 0 deletions src/trie/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,18 @@ mod bench {

const MAP_SIZE: usize = 1000;

map_insert_rand_bench!{insert_rand_100, 100, TrieMap}
map_insert_rand_bench!{insert_rand_10_000, 10_000, TrieMap}

map_insert_seq_bench!{insert_seq_100, 100, TrieMap}
map_insert_seq_bench!{insert_seq_10_000, 10_000, TrieMap}

map_find_rand_bench!{find_rand_100, 100, TrieMap}
map_find_rand_bench!{find_rand_10_000, 10_000, TrieMap}

map_find_seq_bench!{find_seq_100, 100, TrieMap}
map_find_seq_bench!{find_seq_10_000, 10_000, TrieMap}

fn random_map(size: usize) -> TrieMap<usize> {
let mut map = TrieMap::<usize>::new();
let mut rng = weak_rng();
Expand Down

0 comments on commit 85859a1

Please sign in to comment.