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)]
15 extern crate collections
;
18 use std
::collections
::BTreeSet
;
19 use std
::collections
::BitSet
;
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) }
55 impl MutableSet
<usize> for BitSet
{
56 fn insert(&mut self, k
: usize) { self.insert(k); }
57 fn remove(&mut self, k
: &usize) -> bool { self.remove(k) }
58 fn contains(&self, k
: &usize) -> bool { self.contains(k) }
62 pub fn bench_int
<T
:MutableSet
<usize>,
72 timed(&mut self.sequential_ints
, || {
73 for i
in 0..num_keys
{
77 for i
in 0..num_keys
{
78 assert
!(set
.contains(&i
));
85 timed(&mut self.random_ints
, || {
86 for _
in 0..num_keys
{
87 set
.insert(rng
.gen
::<usize>() % rand_cap
);
94 for i
in 0..num_keys
{
98 timed(&mut self.delete_ints
, || {
99 for i
in 0..num_keys
{
100 assert
!(set
.remove(&i
));
106 pub fn bench_str
<T
:MutableSet
<String
>,
115 timed(&mut self.sequential_strings
, || {
116 for i
in 0..num_keys
{
117 set
.insert(i
.to_string());
120 for i
in 0..num_keys
{
121 assert
!(set
.contains(&i
.to_string()));
128 timed(&mut self.random_strings
, || {
129 for _
in 0..num_keys
{
130 let s
= rng
.gen
::<usize>().to_string();
138 for i
in 0..num_keys
{
139 set
.insert(i
.to_string());
141 timed(&mut self.delete_strings
, || {
142 for i
in 0..num_keys
{
143 assert
!(set
.remove(&i
.to_string()));
150 fn write_header(header
: &str) {
151 println
!("{}", header
);
154 fn write_row(label
: &str, value
: Duration
) {
155 println
!("{:30} {} s\n", label
, value
);
158 fn write_results(label
: &str, results
: &Results
) {
160 write_row("sequential_ints", results
.sequential_ints
);
161 write_row("random_ints", results
.random_ints
);
162 write_row("delete_ints", results
.delete_ints
);
163 write_row("sequential_strings", results
.sequential_strings
);
164 write_row("random_strings", results
.random_strings
);
165 write_row("delete_strings", results
.delete_strings
);
168 fn empty_results() -> Results
{
170 sequential_ints
: Duration
::new(0, 0),
171 random_ints
: Duration
::new(0, 0),
172 delete_ints
: Duration
::new(0, 0),
174 sequential_strings
: Duration
::new(0, 0),
175 random_strings
: Duration
::new(0, 0),
176 delete_strings
: Duration
::new(0, 0),
181 let mut args
= env
::args();
184 args
.nth(1).unwrap().parse
::<usize>().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
<usize> = 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
<usize> = 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
, || BitSet
::new());
225 write_results("collections::bit_vec::BitSet", &results
);