]> git.lizzy.rs Git - rust.git/blob - src/test/bench/std-smallintmap.rs
Merge pull request #20510 from tshepang/patch-6
[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 use std::collections::VecMap;
14 use std::os;
15 use std::time::Duration;
16 use std::uint;
17
18 fn append_sequential(min: uint, max: uint, map: &mut VecMap<uint>) {
19     for i in range(min, max) {
20         map.insert(i, i + 22u);
21     }
22 }
23
24 fn check_sequential(min: uint, max: uint, map: &VecMap<uint>) {
25     for i in range(min, max) {
26         assert_eq!(map[i], i + 22u);
27     }
28 }
29
30 fn main() {
31     let args = os::args();
32     let args = if os::getenv("RUST_BENCH").is_some() {
33         vec!("".to_string(), "100000".to_string(), "100".to_string())
34     } else if args.len() <= 1u {
35         vec!("".to_string(), "10000".to_string(), "50".to_string())
36     } else {
37         args.into_iter().collect()
38     };
39     let max = args[1].parse::<uint>().unwrap();
40     let rep = args[2].parse::<uint>().unwrap();
41
42     let mut checkf = Duration::seconds(0);
43     let mut appendf = Duration::seconds(0);
44
45     for _ in range(0u, rep) {
46         let mut map = VecMap::new();
47         let d1 = Duration::span(|| append_sequential(0u, max, &mut map));
48         let d2 = Duration::span(|| check_sequential(0u, max, &map));
49
50         checkf = checkf + d2;
51         appendf = appendf + d1;
52     }
53
54     let maxf = max as f64;
55
56     println!("insert(): {} seconds\n", checkf);
57     println!("        : {} op/ms\n", maxf / checkf.num_milliseconds() as f64);
58     println!("get()   : {} seconds\n", appendf);
59     println!("        : {} op/ms\n", maxf / appendf.num_milliseconds() as f64);
60 }