]> git.lizzy.rs Git - rust.git/blob - src/test/bench/std-smallintmap.rs
test: Make manual changes to deal with the fallout from removal of
[rust.git] / src / test / bench / std-smallintmap.rs
1 // Copyright 2012 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 // Microbenchmark for the smallintmap library
12
13 extern crate collections;
14 extern crate time;
15
16 use collections::SmallIntMap;
17 use std::os;
18 use std::uint;
19 use std::vec_ng::Vec;
20
21 fn append_sequential(min: uint, max: uint, map: &mut SmallIntMap<uint>) {
22     for i in range(min, max) {
23         map.insert(i, i + 22u);
24     }
25 }
26
27 fn check_sequential(min: uint, max: uint, map: &SmallIntMap<uint>) {
28     for i in range(min, max) {
29         assert_eq!(*map.get(&i), i + 22u);
30     }
31 }
32
33 fn main() {
34     let args = os::args();
35     let args = if os::getenv("RUST_BENCH").is_some() {
36         vec!(~"", ~"100000", ~"100")
37     } else if args.len() <= 1u {
38         vec!(~"", ~"10000", ~"50")
39     } else {
40         args.move_iter().collect()
41     };
42     let max = from_str::<uint>(*args.get(1)).unwrap();
43     let rep = from_str::<uint>(*args.get(2)).unwrap();
44
45     let mut checkf = 0.0;
46     let mut appendf = 0.0;
47
48     for _ in range(0u, rep) {
49         let mut map = SmallIntMap::new();
50         let start = time::precise_time_s();
51         append_sequential(0u, max, &mut map);
52         let mid = time::precise_time_s();
53         check_sequential(0u, max, &map);
54         let end = time::precise_time_s();
55
56         checkf += (end - mid) as f64;
57         appendf += (mid - start) as f64;
58     }
59
60     let maxf = max as f64;
61
62     println!("insert(): {:?} seconds\n", checkf);
63     println!("        : {} op/sec\n", maxf/checkf);
64     println!("get()   : {:?} seconds\n", appendf);
65     println!("        : {} op/sec\n", maxf/appendf);
66 }