]> git.proxmox.com Git - rustc.git/blame - src/test/bench/core-set.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / test / bench / core-set.rs
CommitLineData
1a4d82fc 1// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
223e47cc
LB
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
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.
10
1a4d82fc 11// ignore-pretty very bad with line comments
970d7e83 12
d9579d0f 13#![feature(unboxed_closures, rand, std_misc, collections, duration, duration_span)]
62682a34 14#![feature(bitset)]
1a4d82fc
JJ
15
16extern crate collections;
17extern crate rand;
18
19use std::collections::BTreeSet;
85aaf69f 20use std::collections::BitSet;
1a4d82fc 21use std::collections::HashSet;
1a4d82fc 22use std::hash::Hash;
85aaf69f 23use std::env;
1a4d82fc 24use std::time::Duration;
223e47cc
LB
25
26struct Results {
1a4d82fc
JJ
27 sequential_ints: Duration,
28 random_ints: Duration,
29 delete_ints: Duration,
30
31 sequential_strings: Duration,
32 random_strings: Duration,
33 delete_strings: Duration,
34}
223e47cc 35
1a4d82fc
JJ
36fn timed<F>(result: &mut Duration, op: F) where F: FnOnce() {
37 *result = Duration::span(op);
223e47cc
LB
38}
39
1a4d82fc
JJ
40trait MutableSet<T> {
41 fn insert(&mut self, k: T);
42 fn remove(&mut self, k: &T) -> bool;
43 fn contains(&self, k: &T) -> bool;
44}
45
85aaf69f 46impl<T: Hash + Eq> MutableSet<T> for HashSet<T> {
1a4d82fc
JJ
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) }
50}
51impl<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) }
55}
85aaf69f
SL
56impl MutableSet<usize> for BitSet {
57 fn insert(&mut self, k: usize) { self.insert(k); }
58 fn remove(&mut self, k: &usize) -> bool { self.remove(k) }
59 fn contains(&self, k: &usize) -> bool { self.contains(k) }
223e47cc
LB
60}
61
970d7e83 62impl Results {
85aaf69f 63 pub fn bench_int<T:MutableSet<usize>,
1a4d82fc
JJ
64 R:rand::Rng,
65 F:FnMut() -> T>(
66 &mut self,
67 rng: &mut R,
85aaf69f
SL
68 num_keys: usize,
69 rand_cap: usize,
1a4d82fc 70 mut f: F) {
223e47cc
LB
71 {
72 let mut set = f();
1a4d82fc 73 timed(&mut self.sequential_ints, || {
85aaf69f 74 for i in 0..num_keys {
223e47cc
LB
75 set.insert(i);
76 }
77
85aaf69f 78 for i in 0..num_keys {
223e47cc
LB
79 assert!(set.contains(&i));
80 }
1a4d82fc 81 })
223e47cc
LB
82 }
83
84 {
85 let mut set = f();
1a4d82fc 86 timed(&mut self.random_ints, || {
85aaf69f
SL
87 for _ in 0..num_keys {
88 set.insert(rng.gen::<usize>() % rand_cap);
223e47cc 89 }
1a4d82fc 90 })
223e47cc
LB
91 }
92
93 {
94 let mut set = f();
85aaf69f 95 for i in 0..num_keys {
223e47cc
LB
96 set.insert(i);
97 }
98
1a4d82fc 99 timed(&mut self.delete_ints, || {
85aaf69f 100 for i in 0..num_keys {
223e47cc
LB
101 assert!(set.remove(&i));
102 }
1a4d82fc 103 })
223e47cc
LB
104 }
105 }
106
1a4d82fc
JJ
107 pub fn bench_str<T:MutableSet<String>,
108 R:rand::Rng,
109 F:FnMut() -> T>(
110 &mut self,
111 rng: &mut R,
85aaf69f 112 num_keys: usize,
1a4d82fc 113 mut f: F) {
223e47cc
LB
114 {
115 let mut set = f();
1a4d82fc 116 timed(&mut self.sequential_strings, || {
85aaf69f 117 for i in 0..num_keys {
1a4d82fc 118 set.insert(i.to_string());
223e47cc
LB
119 }
120
85aaf69f 121 for i in 0..num_keys {
1a4d82fc 122 assert!(set.contains(&i.to_string()));
223e47cc 123 }
1a4d82fc 124 })
223e47cc
LB
125 }
126
127 {
128 let mut set = f();
1a4d82fc 129 timed(&mut self.random_strings, || {
85aaf69f
SL
130 for _ in 0..num_keys {
131 let s = rng.gen::<usize>().to_string();
223e47cc
LB
132 set.insert(s);
133 }
1a4d82fc 134 })
223e47cc
LB
135 }
136
137 {
138 let mut set = f();
85aaf69f 139 for i in 0..num_keys {
1a4d82fc 140 set.insert(i.to_string());
223e47cc 141 }
1a4d82fc 142 timed(&mut self.delete_strings, || {
85aaf69f 143 for i in 0..num_keys {
1a4d82fc 144 assert!(set.remove(&i.to_string()));
223e47cc 145 }
1a4d82fc 146 })
223e47cc
LB
147 }
148 }
149}
150
151fn write_header(header: &str) {
1a4d82fc 152 println!("{}", header);
223e47cc
LB
153}
154
1a4d82fc 155fn write_row(label: &str, value: Duration) {
c1a9b12d 156 println!("{:30} {:?} s\n", label, value);
223e47cc
LB
157}
158
159fn write_results(label: &str, results: &Results) {
160 write_header(label);
161 write_row("sequential_ints", results.sequential_ints);
162 write_row("random_ints", results.random_ints);
163 write_row("delete_ints", results.delete_ints);
164 write_row("sequential_strings", results.sequential_strings);
165 write_row("random_strings", results.random_strings);
166 write_row("delete_strings", results.delete_strings);
167}
168
169fn empty_results() -> Results {
170 Results {
d9579d0f
AL
171 sequential_ints: Duration::new(0, 0),
172 random_ints: Duration::new(0, 0),
173 delete_ints: Duration::new(0, 0),
223e47cc 174
d9579d0f
AL
175 sequential_strings: Duration::new(0, 0),
176 random_strings: Duration::new(0, 0),
177 delete_strings: Duration::new(0, 0),
223e47cc
LB
178 }
179}
180
181fn main() {
85aaf69f 182 let mut args = env::args();
223e47cc
LB
183 let num_keys = {
184 if args.len() == 2 {
85aaf69f 185 args.nth(1).unwrap().parse::<usize>().unwrap()
223e47cc
LB
186 } else {
187 100 // woefully inadequate for any real measurement
188 }
189 };
190
1a4d82fc 191 let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
223e47cc
LB
192 let max = 200000;
193
194 {
1a4d82fc 195 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
223e47cc 196 let mut results = empty_results();
1a4d82fc 197 results.bench_int(&mut rng, num_keys, max, || {
85aaf69f 198 let s: HashSet<usize> = HashSet::new();
1a4d82fc
JJ
199 s
200 });
201 results.bench_str(&mut rng, num_keys, || {
202 let s: HashSet<String> = HashSet::new();
203 s
204 });
205 write_results("collections::HashSet", &results);
223e47cc
LB
206 }
207
208 {
1a4d82fc 209 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
223e47cc 210 let mut results = empty_results();
1a4d82fc 211 results.bench_int(&mut rng, num_keys, max, || {
85aaf69f 212 let s: BTreeSet<usize> = BTreeSet::new();
1a4d82fc
JJ
213 s
214 });
215 results.bench_str(&mut rng, num_keys, || {
216 let s: BTreeSet<String> = BTreeSet::new();
217 s
218 });
219 write_results("collections::BTreeSet", &results);
223e47cc
LB
220 }
221
222 {
1a4d82fc 223 let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(seed);
223e47cc 224 let mut results = empty_results();
85aaf69f
SL
225 results.bench_int(&mut rng, num_keys, max, || BitSet::new());
226 write_results("collections::bit_vec::BitSet", &results);
223e47cc
LB
227 }
228}