]> git.proxmox.com Git - rustc.git/blob - src/test/bench/std-smallintmap.rs
6a39a6db0c7284769937e2e07c19db7d8600d916
[rustc.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 #![feature(vecmap, duration, duration_span)]
14
15 use std::collections::VecMap;
16 use std::env;
17 use std::time::Duration;
18
19 fn append_sequential(min: usize, max: usize, map: &mut VecMap<usize>) {
20 for i in min..max {
21 map.insert(i, i + 22);
22 }
23 }
24
25 fn check_sequential(min: usize, max: usize, map: &VecMap<usize>) {
26 for i in min..max {
27 assert_eq!(map[i], i + 22);
28 }
29 }
30
31 fn main() {
32 let args = env::args();
33 let args = if env::var_os("RUST_BENCH").is_some() {
34 vec!("".to_string(), "100000".to_string(), "100".to_string())
35 } else if args.len() <= 1 {
36 vec!("".to_string(), "10000".to_string(), "50".to_string())
37 } else {
38 args.collect()
39 };
40 let max = args[1].parse::<usize>().unwrap();
41 let rep = args[2].parse::<usize>().unwrap();
42
43 let mut checkf = Duration::new(0, 0);
44 let mut appendf = Duration::new(0, 0);
45
46 for _ in 0..rep {
47 let mut map = VecMap::new();
48 let d1 = Duration::span(|| append_sequential(0, max, &mut map));
49 let d2 = Duration::span(|| check_sequential(0, 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/s\n", maxf / checkf.secs() as f64);
59 println!("get() : {} seconds\n", appendf);
60 println!(" : {} op/s\n", maxf / appendf.secs() as f64);
61 }