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;
16 use collections::SmallIntMap;
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);
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);
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())
39 args.move_iter().collect()
41 let max = from_str::<uint>(*args.get(1)).unwrap();
42 let rep = from_str::<uint>(*args.get(2)).unwrap();
45 let mut appendf = 0.0;
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();
55 checkf += (end - mid) as f64;
56 appendf += (mid - start) as f64;
59 let maxf = max as f64;
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);