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::time::Duration;
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);
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);
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())
37 args.into_iter().collect()
39 let max = args[1].parse::<uint>().unwrap();
40 let rep = args[2].parse::<uint>().unwrap();
42 let mut checkf = Duration::seconds(0);
43 let mut appendf = Duration::seconds(0);
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));
51 appendf = appendf + d1;
54 let maxf = max as f64;
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);