|
| 1 | +// Copyright 2013 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. |
| 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 | + |
| 11 | +extern mod std; |
| 12 | +use core::hashmap::linear::LinearSet; |
| 13 | +use std::bitv::BitvSet; |
| 14 | +use std::treemap::TreeSet; |
| 15 | +use core::io::WriterUtil; |
| 16 | + |
| 17 | +struct Results { |
| 18 | + sequential_ints: float, |
| 19 | + random_ints: float, |
| 20 | + delete_ints: float, |
| 21 | + |
| 22 | + sequential_strings: float, |
| 23 | + random_strings: float, |
| 24 | + delete_strings: float |
| 25 | +} |
| 26 | + |
| 27 | +fn timed(result: &mut float, op: fn()) { |
| 28 | + let start = std::time::precise_time_s(); |
| 29 | + op(); |
| 30 | + let end = std::time::precise_time_s(); |
| 31 | + *result = (end - start); |
| 32 | +} |
| 33 | + |
| 34 | +impl Results { |
| 35 | + fn bench_int<T: Set<uint>>(&mut self, rng: @rand::Rng, num_keys: uint, |
| 36 | + rand_cap: uint, f: fn() -> T) { |
| 37 | + { |
| 38 | + let mut set = f(); |
| 39 | + do timed(&mut self.sequential_ints) { |
| 40 | + for uint::range(0, num_keys) |i| { |
| 41 | + set.insert(i); |
| 42 | + } |
| 43 | + |
| 44 | + for uint::range(0, num_keys) |i| { |
| 45 | + assert set.contains(&i); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + { |
| 51 | + let mut set = f(); |
| 52 | + do timed(&mut self.random_ints) { |
| 53 | + for num_keys.times { |
| 54 | + set.insert((rng.next() as uint) % rand_cap); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + { |
| 60 | + let mut set = f(); |
| 61 | + for uint::range(0, num_keys) |i| { |
| 62 | + set.insert(i); |
| 63 | + } |
| 64 | + |
| 65 | + do timed(&mut self.delete_ints) { |
| 66 | + for uint::range(0, num_keys) |i| { |
| 67 | + assert set.remove(&i); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + fn bench_str<T: Set<~str>>(&mut self, rng: @rand::Rng, num_keys: uint, |
| 74 | + f: fn() -> T) { |
| 75 | + { |
| 76 | + let mut set = f(); |
| 77 | + do timed(&mut self.sequential_strings) { |
| 78 | + for uint::range(0, num_keys) |i| { |
| 79 | + let s = uint::to_str(i); |
| 80 | + set.insert(s); |
| 81 | + } |
| 82 | + |
| 83 | + for uint::range(0, num_keys) |i| { |
| 84 | + let s = uint::to_str(i); |
| 85 | + assert set.contains(&s); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + { |
| 91 | + let mut set = f(); |
| 92 | + do timed(&mut self.random_strings) { |
| 93 | + for num_keys.times { |
| 94 | + let s = uint::to_str(rng.next() as uint); |
| 95 | + set.insert(s); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + { |
| 101 | + let mut set = f(); |
| 102 | + for uint::range(0, num_keys) |i| { |
| 103 | + set.insert(uint::to_str(i)); |
| 104 | + } |
| 105 | + do timed(&mut self.delete_strings) { |
| 106 | + for uint::range(0, num_keys) |i| { |
| 107 | + assert set.remove(&uint::to_str(i)); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +fn write_header(header: &str) { |
| 115 | + io::stdout().write_str(header); |
| 116 | + io::stdout().write_str("\n"); |
| 117 | +} |
| 118 | + |
| 119 | +fn write_row(label: &str, value: float) { |
| 120 | + io::stdout().write_str(fmt!("%30s %f s\n", label, value)); |
| 121 | +} |
| 122 | + |
| 123 | +fn write_results(label: &str, results: &Results) { |
| 124 | + write_header(label); |
| 125 | + write_row("sequential_ints", results.sequential_ints); |
| 126 | + write_row("random_ints", results.random_ints); |
| 127 | + write_row("delete_ints", results.delete_ints); |
| 128 | + write_row("sequential_strings", results.sequential_strings); |
| 129 | + write_row("random_strings", results.random_strings); |
| 130 | + write_row("delete_strings", results.delete_strings); |
| 131 | +} |
| 132 | + |
| 133 | +fn empty_results() -> Results { |
| 134 | + Results { |
| 135 | + sequential_ints: 0f, |
| 136 | + random_ints: 0f, |
| 137 | + delete_ints: 0f, |
| 138 | + |
| 139 | + sequential_strings: 0f, |
| 140 | + random_strings: 0f, |
| 141 | + delete_strings: 0f, |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +fn main() { |
| 146 | + let args = os::args(); |
| 147 | + let num_keys = { |
| 148 | + if args.len() == 2 { |
| 149 | + uint::from_str(args[1]).get() |
| 150 | + } else { |
| 151 | + 100 // woefully inadequate for any real measurement |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + let seed = ~[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 156 | + let max = 200000; |
| 157 | + |
| 158 | + { |
| 159 | + let rng = rand::seeded_rng(&seed); |
| 160 | + let mut results = empty_results(); |
| 161 | + results.bench_int(rng, num_keys, max, || LinearSet::new::<uint>()); |
| 162 | + results.bench_str(rng, num_keys, || LinearSet::new::<~str>()); |
| 163 | + write_results("core::hashmap::LinearSet", &results); |
| 164 | + } |
| 165 | + |
| 166 | + { |
| 167 | + let rng = rand::seeded_rng(&seed); |
| 168 | + let mut results = empty_results(); |
| 169 | + results.bench_int(rng, num_keys, max, || TreeSet::new::<uint>()); |
| 170 | + results.bench_str(rng, num_keys, || TreeSet::new::<~str>()); |
| 171 | + write_results("std::treemap::TreeSet", &results); |
| 172 | + } |
| 173 | + |
| 174 | + { |
| 175 | + let rng = rand::seeded_rng(&seed); |
| 176 | + let mut results = empty_results(); |
| 177 | + results.bench_int(rng, num_keys, max, || BitvSet::new()); |
| 178 | + write_results("std::bitv::BitvSet", &results); |
| 179 | + } |
| 180 | +} |
0 commit comments