summary refs log tree commit diff
path: root/src/test/bench/std-smallintmap.rs
blob: b9ed1576e37165a5f03248430c3158b8c34e840c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Microbenchmark for the smallintmap library

extern crate collections;
extern crate time;

use collections::SmallIntMap;
use std::os;
use std::uint;

fn append_sequential(min: uint, max: uint, map: &mut SmallIntMap<uint>) {
    for i in range(min, max) {
        map.insert(i, i + 22u);
    }
}

fn check_sequential(min: uint, max: uint, map: &SmallIntMap<uint>) {
    for i in range(min, max) {
        assert_eq!(*map.get(&i), i + 22u);
    }
}

fn main() {
    let args = os::args();
    let args = if os::getenv("RUST_BENCH").is_some() {
        vec!(~"", ~"100000", ~"100")
    } else if args.len() <= 1u {
        vec!(~"", ~"10000", ~"50")
    } else {
        args.move_iter().collect()
    };
    let max = from_str::<uint>(*args.get(1)).unwrap();
    let rep = from_str::<uint>(*args.get(2)).unwrap();

    let mut checkf = 0.0;
    let mut appendf = 0.0;

    for _ in range(0u, rep) {
        let mut map = SmallIntMap::new();
        let start = time::precise_time_s();
        append_sequential(0u, max, &mut map);
        let mid = time::precise_time_s();
        check_sequential(0u, max, &map);
        let end = time::precise_time_s();

        checkf += (end - mid) as f64;
        appendf += (mid - start) as f64;
    }

    let maxf = max as f64;

    println!("insert(): {:?} seconds\n", checkf);
    println!("        : {} op/sec\n", maxf/checkf);
    println!("get()   : {:?} seconds\n", appendf);
    println!("        : {} op/sec\n", maxf/appendf);
}