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 use std::collections::VecMap;
15 use std::str::from_str;
16 use std::time::Duration;
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);
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);
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())
38 args.into_iter().collect()
40 let max = from_str::<uint>(args[1].as_slice()).unwrap();
41 let rep = from_str::<uint>(args[2].as_slice()).unwrap();
43 let mut checkf = Duration::seconds(0);
44 let mut appendf = Duration::seconds(0);
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));
52 appendf = appendf + d1;
55 let maxf = max as f64;
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);