rust/src/test/bench/core-map.rs

169 lines
4.2 KiB
Rust
Raw Normal View History

// Copyright 2013 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.
2014-12-08 07:35:34 -06:00
#![feature(unboxed_closures)]
2014-12-16 22:09:16 -06:00
use std::collections::{BTreeMap, HashMap, HashSet};
use std::os;
use std::rand::{Rng, IsaacRng, SeedableRng};
use std::time::Duration;
use std::uint;
2014-12-08 07:35:34 -06:00
fn timed<F>(label: &str, f: F) where F: FnMut() {
println!(" {}: {}", label, Duration::span(f));
}
trait MutableMap {
fn insert(&mut self, k: uint, v: uint);
fn remove(&mut self, k: &uint) -> bool;
fn find(&self, k: &uint) -> Option<&uint>;
}
2014-12-16 22:09:16 -06:00
impl MutableMap for BTreeMap<uint, uint> {
fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
2014-11-06 11:25:16 -06:00
fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
}
impl MutableMap for HashMap<uint, uint> {
fn insert(&mut self, k: uint, v: uint) { self.insert(k, v); }
2014-11-06 11:25:16 -06:00
fn remove(&mut self, k: &uint) -> bool { self.remove(k).is_some() }
fn find(&self, k: &uint) -> Option<&uint> { self.get(k) }
}
fn ascending<M: MutableMap>(map: &mut M, n_keys: uint) {
println!(" Ascending integers:");
timed("insert", || {
for i in range(0u, n_keys) {
map.insert(i, i + 1);
}
});
timed("search", || {
for i in range(0u, n_keys) {
assert_eq!(map.find(&i).unwrap(), &(i + 1));
}
});
timed("remove", || {
for i in range(0, n_keys) {
2013-03-28 20:39:09 -05:00
assert!(map.remove(&i));
}
});
}
fn descending<M: MutableMap>(map: &mut M, n_keys: uint) {
println!(" Descending integers:");
timed("insert", || {
for i in range(0, n_keys).rev() {
map.insert(i, i + 1);
}
});
timed("search", || {
for i in range(0, n_keys).rev() {
assert_eq!(map.find(&i).unwrap(), &(i + 1));
}
});
timed("remove", || {
for i in range(0, n_keys) {
2013-03-28 20:39:09 -05:00
assert!(map.remove(&i));
}
});
}
fn vector<M: MutableMap>(map: &mut M, n_keys: uint, dist: &[uint]) {
timed("insert", || {
for i in range(0u, n_keys) {
map.insert(dist[i], i + 1);
2013-01-23 18:51:49 -06:00
}
});
2013-01-23 18:51:49 -06:00
timed("search", || {
for i in range(0u, n_keys) {
assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1));
2013-01-23 18:51:49 -06:00
}
});
2013-01-23 18:51:49 -06:00
timed("remove", || {
for i in range(0u, n_keys) {
2013-03-28 20:39:09 -05:00
assert!(map.remove(&dist[i]));
2013-01-23 18:51:49 -06:00
}
});
2013-01-23 18:51:49 -06:00
}
fn main() {
let args = os::args();
2014-05-05 02:29:59 -05:00
let args = args.as_slice();
let n_keys = {
if args.len() == 2 {
args[1].parse::<uint>().unwrap()
} else {
1000000
2013-01-23 18:51:49 -06:00
}
};
2013-01-23 18:51:49 -06:00
let mut rand = Vec::with_capacity(n_keys);
2013-01-23 18:51:49 -06:00
{
let seed: &[_] = &[1, 1, 1, 1, 1, 1, 1];
let mut rng: IsaacRng = SeedableRng::from_seed(seed);
let mut set = HashSet::new();
while set.len() != n_keys {
let next = rng.gen();
if set.insert(next) {
rand.push(next);
2013-01-23 18:51:49 -06:00
}
}
}
println!("{} keys", n_keys);
2013-01-23 18:51:49 -06:00
// FIXME: #9970
2014-12-16 22:09:16 -06:00
println!("{}", "\nBTreeMap:");
2013-01-23 18:51:49 -06:00
{
2014-12-16 22:09:16 -06:00
let mut map: BTreeMap<uint,uint> = BTreeMap::new();
ascending(&mut map, n_keys);
2013-01-23 18:51:49 -06:00
}
{
2014-12-16 22:09:16 -06:00
let mut map: BTreeMap<uint,uint> = BTreeMap::new();
descending(&mut map, n_keys);
2013-01-23 18:51:49 -06:00
}
{
println!(" Random integers:");
2014-12-16 22:09:16 -06:00
let mut map: BTreeMap<uint,uint> = BTreeMap::new();
vector(&mut map, n_keys, rand.as_slice());
2013-01-23 18:51:49 -06:00
}
// FIXME: #9970
println!("{}", "\nHashMap:");
2013-01-23 18:51:49 -06:00
{
2013-08-27 20:45:13 -05:00
let mut map: HashMap<uint,uint> = HashMap::new();
ascending(&mut map, n_keys);
2013-01-23 18:51:49 -06:00
}
{
2013-08-27 20:45:13 -05:00
let mut map: HashMap<uint,uint> = HashMap::new();
descending(&mut map, n_keys);
2013-01-23 18:51:49 -06:00
}
{
println!(" Random integers:");
2013-08-27 20:45:13 -05:00
let mut map: HashMap<uint,uint> = HashMap::new();
vector(&mut map, n_keys, rand.as_slice());
}
2012-12-28 21:57:18 -06:00
}