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.
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.
11 // Microbenchmark for the smallintmap library
13 extern crate collections;
17 use std::collections::SmallIntMap;
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);
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);
34 let args = os::args();
35 let args = if os::getenv("RUST_BENCH").is_some() {
36 vec!("".to_string(), "100000".to_string(), "100".to_string())
37 } else if args.len() <= 1u {
38 vec!("".to_string(), "10000".to_string(), "50".to_string())
40 args.move_iter().collect()
42 let max = from_str::<uint>(args.get(1).as_slice()).unwrap();
43 let rep = from_str::<uint>(args.get(2).as_slice()).unwrap();
46 let mut appendf = 0.0;
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();
56 checkf += (end - mid) as f64;
57 appendf += (mid - start) as f64;
60 let maxf = max as f64;
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);