2014-02-07 13:08:32 -06:00
|
|
|
// ignore-pretty
|
2013-11-24 13:44:28 -06:00
|
|
|
|
2014-02-07 13:08:32 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-02-18 00:24:14 -06:00
|
|
|
// 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.
|
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
extern mod extra;
|
2014-02-02 23:56:49 -06:00
|
|
|
extern mod collections;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2014-02-02 23:56:49 -06:00
|
|
|
use collections::bitv::BitvSet;
|
|
|
|
use collections::TreeSet;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::hashmap::HashSet;
|
|
|
|
use std::os;
|
|
|
|
use std::rand;
|
|
|
|
use std::uint;
|
2013-02-18 00:24:14 -06:00
|
|
|
|
|
|
|
struct Results {
|
2013-09-26 01:26:09 -05:00
|
|
|
sequential_ints: f64,
|
|
|
|
random_ints: f64,
|
|
|
|
delete_ints: f64,
|
2013-02-18 00:24:14 -06:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
sequential_strings: f64,
|
|
|
|
random_strings: f64,
|
|
|
|
delete_strings: f64
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
2013-11-19 18:34:19 -06:00
|
|
|
fn timed(result: &mut f64, op: ||) {
|
2013-05-20 19:07:24 -05:00
|
|
|
let start = extra::time::precise_time_s();
|
2013-02-18 00:24:14 -06:00
|
|
|
op();
|
2013-05-20 19:07:24 -05:00
|
|
|
let end = extra::time::precise_time_s();
|
2013-02-18 00:24:14 -06:00
|
|
|
*result = (end - start);
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Results {
|
2013-07-13 22:30:05 -05:00
|
|
|
pub fn bench_int<T:MutableSet<uint>,
|
2013-11-19 18:34:19 -06:00
|
|
|
R: rand::Rng>(
|
|
|
|
&mut self,
|
|
|
|
rng: &mut R,
|
|
|
|
num_keys: uint,
|
|
|
|
rand_cap: uint,
|
|
|
|
f: || -> T) { {
|
2013-02-18 00:24:14 -06:00
|
|
|
let mut set = f();
|
2013-11-21 21:20:48 -06:00
|
|
|
timed(&mut self.sequential_ints, || {
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, num_keys) {
|
2013-02-18 00:24:14 -06:00
|
|
|
set.insert(i);
|
|
|
|
}
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, num_keys) {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(set.contains(&i));
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut set = f();
|
2013-11-21 21:20:48 -06:00
|
|
|
timed(&mut self.random_ints, || {
|
2013-08-05 22:43:06 -05:00
|
|
|
for _ in range(0, num_keys) {
|
2013-09-21 07:06:50 -05:00
|
|
|
set.insert(rng.gen::<uint>() % rand_cap);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut set = f();
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, num_keys) {
|
2013-02-18 00:24:14 -06:00
|
|
|
set.insert(i);
|
|
|
|
}
|
|
|
|
|
2013-11-21 21:20:48 -06:00
|
|
|
timed(&mut self.delete_ints, || {
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, num_keys) {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(set.remove(&i));
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 22:30:05 -05:00
|
|
|
pub fn bench_str<T:MutableSet<~str>,
|
2013-11-19 18:34:19 -06:00
|
|
|
R:rand::Rng>(
|
|
|
|
&mut self,
|
|
|
|
rng: &mut R,
|
|
|
|
num_keys: uint,
|
|
|
|
f: || -> T) {
|
2013-02-18 00:24:14 -06:00
|
|
|
{
|
|
|
|
let mut set = f();
|
2013-11-21 21:20:48 -06:00
|
|
|
timed(&mut self.sequential_strings, || {
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, num_keys) {
|
2013-08-17 21:47:54 -05:00
|
|
|
set.insert(i.to_str());
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, num_keys) {
|
2013-08-17 21:47:54 -05:00
|
|
|
assert!(set.contains(&i.to_str()));
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut set = f();
|
2013-11-21 21:20:48 -06:00
|
|
|
timed(&mut self.random_strings, || {
|
2013-08-05 22:43:06 -05:00
|
|
|
for _ in range(0, num_keys) {
|
2013-09-21 07:06:50 -05:00
|
|
|
let s = rng.gen::<uint>().to_str();
|
2013-02-18 00:24:14 -06:00
|
|
|
set.insert(s);
|
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut set = f();
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, num_keys) {
|
2013-08-17 21:47:54 -05:00
|
|
|
set.insert(i.to_str());
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
timed(&mut self.delete_strings, || {
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, num_keys) {
|
2013-08-17 21:47:54 -05:00
|
|
|
assert!(set.remove(&i.to_str()));
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_header(header: &str) {
|
2014-01-09 04:06:55 -06:00
|
|
|
println!("{}", header);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
fn write_row(label: &str, value: f64) {
|
2013-10-13 20:48:47 -05:00
|
|
|
println!("{:30s} {} s\n", label, value);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_results(label: &str, results: &Results) {
|
|
|
|
write_header(label);
|
|
|
|
write_row("sequential_ints", results.sequential_ints);
|
|
|
|
write_row("random_ints", results.random_ints);
|
|
|
|
write_row("delete_ints", results.delete_ints);
|
|
|
|
write_row("sequential_strings", results.sequential_strings);
|
|
|
|
write_row("random_strings", results.random_strings);
|
|
|
|
write_row("delete_strings", results.delete_strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty_results() -> Results {
|
|
|
|
Results {
|
2013-09-26 01:26:09 -05:00
|
|
|
sequential_ints: 0.0,
|
|
|
|
random_ints: 0.0,
|
|
|
|
delete_ints: 0.0,
|
2013-02-18 00:24:14 -06:00
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
sequential_strings: 0.0,
|
|
|
|
random_strings: 0.0,
|
|
|
|
delete_strings: 0.0,
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
|
|
|
let num_keys = {
|
|
|
|
if args.len() == 2 {
|
2013-09-15 00:27:32 -05:00
|
|
|
from_str::<uint>(args[1]).unwrap()
|
2013-02-18 00:24:14 -06:00
|
|
|
} else {
|
|
|
|
100 // woefully inadequate for any real measurement
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-29 10:29:28 -05:00
|
|
|
let seed = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2013-02-18 00:24:14 -06:00
|
|
|
let max = 200000;
|
|
|
|
|
|
|
|
{
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
|
2013-02-18 00:24:14 -06:00
|
|
|
let mut results = empty_results();
|
2013-08-27 20:45:13 -05:00
|
|
|
results.bench_int(&mut rng, num_keys, max, || {
|
|
|
|
let s: HashSet<uint> = HashSet::new();
|
|
|
|
s
|
|
|
|
});
|
|
|
|
results.bench_str(&mut rng, num_keys, || {
|
|
|
|
let s: HashSet<~str> = HashSet::new();
|
|
|
|
s
|
|
|
|
});
|
2013-05-20 19:07:24 -05:00
|
|
|
write_results("std::hashmap::HashSet", &results);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
|
2013-02-18 00:24:14 -06:00
|
|
|
let mut results = empty_results();
|
2013-08-27 20:45:13 -05:00
|
|
|
results.bench_int(&mut rng, num_keys, max, || {
|
|
|
|
let s: TreeSet<uint> = TreeSet::new();
|
|
|
|
s
|
|
|
|
});
|
|
|
|
results.bench_str(&mut rng, num_keys, || {
|
|
|
|
let s: TreeSet<~str> = TreeSet::new();
|
|
|
|
s
|
|
|
|
});
|
2013-05-20 19:07:24 -05:00
|
|
|
write_results("extra::treemap::TreeSet", &results);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-09-29 10:29:28 -05:00
|
|
|
let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
|
2013-02-18 00:24:14 -06:00
|
|
|
let mut results = empty_results();
|
2013-05-06 21:29:04 -05:00
|
|
|
results.bench_int(&mut rng, num_keys, max, || BitvSet::new());
|
2013-05-20 19:07:24 -05:00
|
|
|
write_results("extra::bitv::BitvSet", &results);
|
2013-02-18 00:24:14 -06:00
|
|
|
}
|
|
|
|
}
|