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