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