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)]
15 extern crate collections;
18 use std::collections::BTreeSet;
19 use std::collections::BitvSet;
20 use std::collections::HashSet;
23 use std::str::from_str;
24 use std::time::Duration;
28 sequential_ints: Duration,
29 random_ints: Duration,
30 delete_ints: Duration,
32 sequential_strings: Duration,
33 random_strings: Duration,
34 delete_strings: Duration,
37 fn timed<F>(result: &mut Duration, op: F) where F: FnOnce() {
38 *result = Duration::span(op);
42 fn insert(&mut self, k: T);
43 fn remove(&mut self, k: &T) -> bool;
44 fn contains(&self, k: &T) -> bool;
47 impl<T: Hash + Eq> MutableSet<T> for HashSet<T> {
48 fn insert(&mut self, k: T) { self.insert(k); }
49 fn remove(&mut self, k: &T) -> bool { self.remove(k) }
50 fn contains(&self, k: &T) -> bool { self.contains(k) }
52 impl<T: Ord> MutableSet<T> for BTreeSet<T> {
53 fn insert(&mut self, k: T) { self.insert(k); }
54 fn remove(&mut self, k: &T) -> bool { self.remove(k) }
55 fn contains(&self, k: &T) -> bool { self.contains(k) }
57 impl MutableSet<uint> for BitvSet {
58 fn insert(&mut self, k: uint) { self.insert(k); }
59 fn remove(&mut self, k: &uint) -> bool { self.remove(k) }
60 fn contains(&self, k: &uint) -> bool { self.contains(k) }
64 pub fn bench_int<T:MutableSet<uint>,
72 timed(&mut self.sequential_ints, || {
73 for i in range(0u, num_keys) {
77 for i in range(0u, num_keys) {
78 assert!(set.contains(&i));
85 timed(&mut self.random_ints, || {
86 for _ in range(0, num_keys) {
87 set.insert(rng.gen::<uint>() % rand_cap);
94 for i in range(0u, num_keys) {
98 timed(&mut self.delete_ints, || {
99 for i in range(0u, num_keys) {
100 assert!(set.remove(&i));
106 pub fn bench_str<T:MutableSet<String>,
114 timed(&mut self.sequential_strings, || {
115 for i in range(0u, num_keys) {
116 set.insert(i.to_string());
119 for i in range(0u, num_keys) {
120 assert!(set.contains(&i.to_string()));
127 timed(&mut self.random_strings, || {
128 for _ in range(0, num_keys) {
129 let s = rng.gen::<uint>().to_string();
137 for i in range(0u, num_keys) {
138 set.insert(i.to_string());
140 timed(&mut self.delete_strings, || {
141 for i in range(0u, num_keys) {
142 assert!(set.remove(&i.to_string()));
149 fn write_header(header: &str) {
150 println!("{}", header);
153 fn write_row(label: &str, value: Duration) {
154 println!("{:30} {} s\n", label, value);
157 fn write_results(label: &str, results: &Results) {
159 write_row("sequential_ints", results.sequential_ints);
160 write_row("random_ints", results.random_ints);
161 write_row("delete_ints", results.delete_ints);
162 write_row("sequential_strings", results.sequential_strings);
163 write_row("random_strings", results.random_strings);
164 write_row("delete_strings", results.delete_strings);
167 fn empty_results() -> Results {
169 sequential_ints: Duration::seconds(0),
170 random_ints: Duration::seconds(0),
171 delete_ints: Duration::seconds(0),
173 sequential_strings: Duration::seconds(0),
174 random_strings: Duration::seconds(0),
175 delete_strings: Duration::seconds(0),
180 let args = os::args();
181 let args = args.as_slice();
184 from_str::<uint>(args[1].as_slice()).unwrap()
186 100 // woefully inadequate for any real measurement
190 let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
194 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
195 let mut results = empty_results();
196 results.bench_int(&mut rng, num_keys, max, || {
197 let s: HashSet<uint> = HashSet::new();
200 results.bench_str(&mut rng, num_keys, || {
201 let s: HashSet<String> = HashSet::new();
204 write_results("collections::HashSet", &results);
208 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
209 let mut results = empty_results();
210 results.bench_int(&mut rng, num_keys, max, || {
211 let s: BTreeSet<uint> = BTreeSet::new();
214 results.bench_str(&mut rng, num_keys, || {
215 let s: BTreeSet<String> = BTreeSet::new();
218 write_results("collections::BTreeSet", &results);
222 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
223 let mut results = empty_results();
224 results.bench_int(&mut rng, num_keys, max, || BitvSet::new());
225 write_results("collections::bitv::BitvSet", &results);