]> git.lizzy.rs Git - rust.git/blob - src/libcollections/bench.rs
Rollup merge of #21964 - semarie:openbsd-env, r=alexcrichton
[rust.git] / src / libcollections / bench.rs
1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use prelude::*;
12 use std::rand;
13 use std::rand::Rng;
14 use test::{Bencher, black_box};
15
16 pub fn insert_rand_n<M, I, R>(n: uint,
17                               map: &mut M,
18                               b: &mut Bencher,
19                               mut insert: I,
20                               mut remove: R) where
21     I: FnMut(&mut M, uint),
22     R: FnMut(&mut M, uint),
23 {
24     // setup
25     let mut rng = rand::weak_rng();
26
27     for _ in 0..n {
28         insert(map, rng.gen::<uint>() % n);
29     }
30
31     // measure
32     b.iter(|| {
33         let k = rng.gen::<uint>() % n;
34         insert(map, k);
35         remove(map, k);
36     });
37     black_box(map);
38 }
39
40 pub fn insert_seq_n<M, I, R>(n: uint,
41                              map: &mut M,
42                              b: &mut Bencher,
43                              mut insert: I,
44                              mut remove: R) where
45     I: FnMut(&mut M, uint),
46     R: FnMut(&mut M, uint),
47 {
48     // setup
49     for i in 0u..n {
50         insert(map, i * 2);
51     }
52
53     // measure
54     let mut i = 1;
55     b.iter(|| {
56         insert(map, i);
57         remove(map, i);
58         i = (i + 2) % n;
59     });
60     black_box(map);
61 }
62
63 pub fn find_rand_n<M, T, I, F>(n: uint,
64                                map: &mut M,
65                                b: &mut Bencher,
66                                mut insert: I,
67                                mut find: F) where
68     I: FnMut(&mut M, uint),
69     F: FnMut(&M, uint) -> T,
70 {
71     // setup
72     let mut rng = rand::weak_rng();
73     let mut keys = (0..n).map(|_| rng.gen::<uint>() % n)
74                               .collect::<Vec<_>>();
75
76     for k in &keys {
77         insert(map, *k);
78     }
79
80     rng.shuffle(&mut keys);
81
82     // measure
83     let mut i = 0;
84     b.iter(|| {
85         let t = find(map, keys[i]);
86         i = (i + 1) % n;
87         black_box(t);
88     })
89 }
90
91 pub fn find_seq_n<M, T, I, F>(n: uint,
92                               map: &mut M,
93                               b: &mut Bencher,
94                               mut insert: I,
95                               mut find: F) where
96     I: FnMut(&mut M, uint),
97     F: FnMut(&M, uint) -> T,
98 {
99     // setup
100     for i in 0u..n {
101         insert(map, i);
102     }
103
104     // measure
105     let mut i = 0;
106     b.iter(|| {
107         let x = find(map, i);
108         i = (i + 1) % n;
109         black_box(x);
110     })
111 }