rust/src/test/bench/std-smallintmap.rs

66 lines
2.0 KiB
Rust
Raw Normal View History

// 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
2012-05-15 08:00:55 -05:00
extern crate collections;
2014-02-19 21:08:12 -06:00
extern crate time;
use collections::SmallIntMap;
use std::os;
use std::uint;
2012-05-15 08:00:55 -05:00
fn append_sequential(min: uint, max: uint, map: &mut SmallIntMap<uint>) {
for i in range(min, max) {
2012-05-15 08:00:55 -05:00
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);
2012-05-15 08:00:55 -05:00
}
}
fn main() {
let args = os::args();
let args = if os::getenv("RUST_BENCH").is_some() {
2014-04-15 20:17:48 -05:00
vec!("".to_owned(), "100000".to_owned(), "100".to_owned())
} else if args.len() <= 1u {
2014-04-15 20:17:48 -05:00
vec!("".to_owned(), "10000".to_owned(), "50".to_owned())
} else {
args.move_iter().collect()
};
let max = from_str::<uint>(*args.get(1)).unwrap();
let rep = from_str::<uint>(*args.get(2)).unwrap();
2012-05-15 08:00:55 -05:00
let mut checkf = 0.0;
let mut appendf = 0.0;
for _ in range(0u, rep) {
let mut map = SmallIntMap::new();
2014-02-19 21:08:12 -06:00
let start = time::precise_time_s();
append_sequential(0u, max, &mut map);
2014-02-19 21:08:12 -06:00
let mid = time::precise_time_s();
check_sequential(0u, max, &map);
2014-02-19 21:08:12 -06:00
let end = time::precise_time_s();
2012-05-15 08:00:55 -05:00
checkf += (end - mid) as f64;
appendf += (mid - start) as f64;
2012-05-15 08:00:55 -05:00
}
let maxf = max as f64;
2012-05-15 08:00:55 -05:00
println!("insert(): {:?} seconds\n", checkf);
println!(" : {} op/sec\n", maxf/checkf);
println!("get() : {:?} seconds\n", appendf);
println!(" : {} op/sec\n", maxf/appendf);
}