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::time::Duration;
27 sequential_ints: Duration,
28 random_ints: Duration,
29 delete_ints: Duration,
31 sequential_strings: Duration,
32 random_strings: Duration,
33 delete_strings: Duration,
36 fn timed<F>(result: &mut Duration, op: F) where F: FnOnce() {
37 *result = Duration::span(op);
41 fn insert(&mut self, k: T);
42 fn remove(&mut self, k: &T) -> bool;
43 fn contains(&self, k: &T) -> bool;
46 impl<T: Hash + Eq> MutableSet<T> for HashSet<T> {
47 fn insert(&mut self, k: T) { self.insert(k); }
48 fn remove(&mut self, k: &T) -> bool { self.remove(k) }
49 fn contains(&self, k: &T) -> bool { self.contains(k) }
51 impl<T: Ord> MutableSet<T> for BTreeSet<T> {
52 fn insert(&mut self, k: T) { self.insert(k); }
53 fn remove(&mut self, k: &T) -> bool { self.remove(k) }
54 fn contains(&self, k: &T) -> bool { self.contains(k) }
56 impl MutableSet<uint> for BitvSet {
57 fn insert(&mut self, k: uint) { self.insert(k); }
58 fn remove(&mut self, k: &uint) -> bool { self.remove(k) }
59 fn contains(&self, k: &uint) -> bool { self.contains(k) }
63 pub fn bench_int<T:MutableSet<uint>,
71 timed(&mut self.sequential_ints, || {
72 for i in range(0u, num_keys) {
76 for i in range(0u, num_keys) {
77 assert!(set.contains(&i));
84 timed(&mut self.random_ints, || {
85 for _ in range(0, num_keys) {
86 set.insert(rng.gen::<uint>() % rand_cap);
93 for i in range(0u, num_keys) {
97 timed(&mut self.delete_ints, || {
98 for i in range(0u, num_keys) {
99 assert!(set.remove(&i));
105 pub fn bench_str<T:MutableSet<String>,
113 timed(&mut self.sequential_strings, || {
114 for i in range(0u, num_keys) {
115 set.insert(i.to_string());
118 for i in range(0u, num_keys) {
119 assert!(set.contains(&i.to_string()));
126 timed(&mut self.random_strings, || {
127 for _ in range(0, num_keys) {
128 let s = rng.gen::<uint>().to_string();
136 for i in range(0u, num_keys) {
137 set.insert(i.to_string());
139 timed(&mut self.delete_strings, || {
140 for i in range(0u, num_keys) {
141 assert!(set.remove(&i.to_string()));
148 fn write_header(header: &str) {
149 println!("{}", header);
152 fn write_row(label: &str, value: Duration) {
153 println!("{:30} {} s\n", label, value);
156 fn write_results(label: &str, results: &Results) {
158 write_row("sequential_ints", results.sequential_ints);
159 write_row("random_ints", results.random_ints);
160 write_row("delete_ints", results.delete_ints);
161 write_row("sequential_strings", results.sequential_strings);
162 write_row("random_strings", results.random_strings);
163 write_row("delete_strings", results.delete_strings);
166 fn empty_results() -> Results {
168 sequential_ints: Duration::seconds(0),
169 random_ints: Duration::seconds(0),
170 delete_ints: Duration::seconds(0),
172 sequential_strings: Duration::seconds(0),
173 random_strings: Duration::seconds(0),
174 delete_strings: Duration::seconds(0),
179 let args = os::args();
180 let args = args.as_slice();
183 args[1].parse::<uint>().unwrap()
185 100 // woefully inadequate for any real measurement
189 let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
193 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
194 let mut results = empty_results();
195 results.bench_int(&mut rng, num_keys, max, || {
196 let s: HashSet<uint> = HashSet::new();
199 results.bench_str(&mut rng, num_keys, || {
200 let s: HashSet<String> = HashSet::new();
203 write_results("collections::HashSet", &results);
207 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
208 let mut results = empty_results();
209 results.bench_int(&mut rng, num_keys, max, || {
210 let s: BTreeSet<uint> = BTreeSet::new();
213 results.bench_str(&mut rng, num_keys, || {
214 let s: BTreeSet<String> = BTreeSet::new();
217 write_results("collections::BTreeSet", &results);
221 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
222 let mut results = empty_results();
223 results.bench_int(&mut rng, num_keys, max, || BitvSet::new());
224 write_results("collections::bitv::BitvSet", &results);