summary refs log tree commit diff
path: root/src/test/bench/std-smallintmap.rs
blob: 2ac651862213b4bf991739a868d9cc4656c38b3a (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
// Microbenchmark for the smallintmap library

extern mod std;
use std::smallintmap;
use std::smallintmap::SmallIntMap;
use io::WriterUtil;

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

fn check_sequential(min: uint, max: uint, map: SmallIntMap<uint>) {
    for uint::range(min, max) |i| {
        assert map.get(i) == i + 22u;
    }
}

fn main() {
    let args = os::args();
    let args = if os::getenv(~"RUST_BENCH").is_some() {
        ~[~"", ~"100000", ~"100"]
    } else if args.len() <= 1u {
        ~[~"", ~"10000", ~"50"]
    } else {
        args
    };
    let max = uint::from_str(args[1]).get();
    let rep = uint::from_str(args[2]).get();

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

    for uint::range(0u, rep) |_r| {
        let map = smallintmap::mk();
        let start = std::time::precise_time_s();
        append_sequential(0u, max, map);
        let mid = std::time::precise_time_s();
        check_sequential(0u, max, map);
        let end = std::time::precise_time_s();

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

    let maxf = max as float;

    io::stdout().write_str(fmt!("insert(): %? seconds\n", checkf));
    io::stdout().write_str(fmt!("        : %f op/sec\n", maxf/checkf));
    io::stdout().write_str(fmt!("get()   : %? seconds\n", appendf));
    io::stdout().write_str(fmt!("        : %f op/sec\n", maxf/appendf));
}