1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-pretty very bad with line comments
13 #![feature(unboxed_closures, rand, std_misc, collections, duration, duration_span)]
16 extern crate collections;
19 use std::collections::BTreeSet;
20 use std::collections::HashSet;
23 use std::time::Duration;
26 sequential_ints: Duration,
27 random_ints: Duration,
28 delete_ints: Duration,
30 sequential_strings: Duration,
31 random_strings: Duration,
32 delete_strings: Duration,
35 fn timed<F>(result: &mut Duration, op: F) where F: FnOnce() {
36 *result = Duration::span(op);
40 fn insert(&mut self, k: T);
41 fn remove(&mut self, k: &T) -> bool;
42 fn contains(&self, k: &T) -> bool;
45 impl<T: Hash + Eq> MutableSet<T> for HashSet<T> {
46 fn insert(&mut self, k: T) { self.insert(k); }
47 fn remove(&mut self, k: &T) -> bool { self.remove(k) }
48 fn contains(&self, k: &T) -> bool { self.contains(k) }
50 impl<T: Ord> MutableSet<T> for BTreeSet<T> {
51 fn insert(&mut self, k: T) { self.insert(k); }
52 fn remove(&mut self, k: &T) -> bool { self.remove(k) }
53 fn contains(&self, k: &T) -> bool { self.contains(k) }
57 pub fn bench_int<T:MutableSet<usize>,
67 timed(&mut self.sequential_ints, || {
68 for i in 0..num_keys {
72 for i in 0..num_keys {
73 assert!(set.contains(&i));
80 timed(&mut self.random_ints, || {
81 for _ in 0..num_keys {
82 set.insert(rng.gen::<usize>() % rand_cap);
89 for i in 0..num_keys {
93 timed(&mut self.delete_ints, || {
94 for i in 0..num_keys {
95 assert!(set.remove(&i));
101 pub fn bench_str<T:MutableSet<String>,
110 timed(&mut self.sequential_strings, || {
111 for i in 0..num_keys {
112 set.insert(i.to_string());
115 for i in 0..num_keys {
116 assert!(set.contains(&i.to_string()));
123 timed(&mut self.random_strings, || {
124 for _ in 0..num_keys {
125 let s = rng.gen::<usize>().to_string();
133 for i in 0..num_keys {
134 set.insert(i.to_string());
136 timed(&mut self.delete_strings, || {
137 for i in 0..num_keys {
138 assert!(set.remove(&i.to_string()));
145 fn write_header(header: &str) {
146 println!("{}", header);
149 fn write_row(label: &str, value: Duration) {
150 println!("{:30} {:?} s\n", label, value);
153 fn write_results(label: &str, results: &Results) {
155 write_row("sequential_ints", results.sequential_ints);
156 write_row("random_ints", results.random_ints);
157 write_row("delete_ints", results.delete_ints);
158 write_row("sequential_strings", results.sequential_strings);
159 write_row("random_strings", results.random_strings);
160 write_row("delete_strings", results.delete_strings);
163 fn empty_results() -> Results {
165 sequential_ints: Duration::new(0, 0),
166 random_ints: Duration::new(0, 0),
167 delete_ints: Duration::new(0, 0),
169 sequential_strings: Duration::new(0, 0),
170 random_strings: Duration::new(0, 0),
171 delete_strings: Duration::new(0, 0),
176 let mut args = env::args();
179 args.nth(1).unwrap().parse::<usize>().unwrap()
181 100 // woefully inadequate for any real measurement
185 let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
189 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
190 let mut results = empty_results();
191 results.bench_int(&mut rng, num_keys, max, || {
192 let s: HashSet<usize> = HashSet::new();
195 results.bench_str(&mut rng, num_keys, || {
196 let s: HashSet<String> = HashSet::new();
199 write_results("collections::HashSet", &results);
203 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
204 let mut results = empty_results();
205 results.bench_int(&mut rng, num_keys, max, || {
206 let s: BTreeSet<usize> = BTreeSet::new();
209 results.bench_str(&mut rng, num_keys, || {
210 let s: BTreeSet<String> = BTreeSet::new();
213 write_results("collections::BTreeSet", &results);