]> git.lizzy.rs Git - rust.git/blob - src/libcollections/bench.rs
Merge pull request #20510 from tshepang/patch-6
[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;
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 range(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 }
38
39 pub fn insert_seq_n<M, I, R>(n: uint,
40                              map: &mut M,
41                              b: &mut Bencher,
42                              mut insert: I,
43                              mut remove: R) where
44     I: FnMut(&mut M, uint),
45     R: FnMut(&mut M, uint),
46 {
47     // setup
48     for i in range(0u, n) {
49         insert(map, i * 2);
50     }
51
52     // measure
53     let mut i = 1;
54     b.iter(|| {
55         insert(map, i);
56         remove(map, i);
57         i = (i + 2) % n;
58     })
59 }
60
61 pub fn find_rand_n<M, T, I, F>(n: uint,
62                                map: &mut M,
63                                b: &mut Bencher,
64                                mut insert: I,
65                                mut find: F) where
66     I: FnMut(&mut M, uint),
67     F: FnMut(&M, uint) -> T,
68 {
69     // setup
70     let mut rng = rand::weak_rng();
71     let mut keys = range(0, n).map(|_| rng.gen::<uint>() % n)
72                               .collect::<Vec<_>>();
73
74     for k in keys.iter() {
75         insert(map, *k);
76     }
77
78     rng.shuffle(keys.as_mut_slice());
79
80     // measure
81     let mut i = 0;
82     b.iter(|| {
83         let t = find(map, keys[i]);
84         i = (i + 1) % n;
85         t
86     })
87 }
88
89 pub fn find_seq_n<M, T, I, F>(n: uint,
90                               map: &mut M,
91                               b: &mut Bencher,
92                               mut insert: I,
93                               mut find: F) where
94     I: FnMut(&mut M, uint),
95     F: FnMut(&M, uint) -> T,
96 {
97     // setup
98     for i in range(0u, n) {
99         insert(map, i);
100     }
101
102     // measure
103     let mut i = 0;
104     b.iter(|| {
105         let x = find(map, i);
106         i = (i + 1) % n;
107         x
108     })
109 }