Skip to content

Commit cf2ddf0

Browse files
committed
Add benchmarks to measure differences in bit vectors
1 parent bf8ed45 commit cf2ddf0

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed

src/libstd/bitv.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,12 +869,16 @@ priv impl BitvSet {
869869
#[cfg(test)]
870870
mod tests {
871871
use core::prelude::*;
872+
use std::test::BenchHarness;
872873
873874
use bitv::*;
874875
use bitv;
875876
876877
use core::uint;
877878
use core::vec;
879+
use core::rand;
880+
881+
const bench_bits : uint = 1 << 14;
878882
879883
#[test]
880884
pub fn test_to_str() {
@@ -1419,6 +1423,94 @@ mod tests {
14191423
assert a.remove(&1000);
14201424
assert a.capacity() == uint::bits;
14211425
}
1426+
1427+
fn rng() -> rand::Rng {
1428+
let seed = ~[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
1429+
rand::seeded_rng(&seed)
1430+
}
1431+
1432+
#[bench]
1433+
pub fn bench_uint_small(b: &mut BenchHarness) {
1434+
let r = rng();
1435+
let mut bitv = 0 as uint;
1436+
do b.iter {
1437+
bitv |= (1 << ((r.next() as uint) % uint::bits));
1438+
}
1439+
}
1440+
1441+
#[bench]
1442+
pub fn bench_small_bitv_small(b: &mut BenchHarness) {
1443+
let r = rng();
1444+
let mut bitv = SmallBitv::new(uint::bits);
1445+
do b.iter {
1446+
bitv.set((r.next() as uint) % uint::bits, true);
1447+
}
1448+
}
1449+
1450+
#[bench]
1451+
pub fn bench_big_bitv_small(b: &mut BenchHarness) {
1452+
let r = rng();
1453+
let mut bitv = BigBitv::new(~[0]);
1454+
do b.iter {
1455+
bitv.set((r.next() as uint) % uint::bits, true);
1456+
}
1457+
}
1458+
1459+
#[bench]
1460+
pub fn bench_big_bitv_big(b: &mut BenchHarness) {
1461+
let r = rng();
1462+
let mut storage = ~[];
1463+
storage.grow(bench_bits / uint::bits, &0);
1464+
let mut bitv = BigBitv::new(storage);
1465+
do b.iter {
1466+
bitv.set((r.next() as uint) % bench_bits, true);
1467+
}
1468+
}
1469+
1470+
#[bench]
1471+
pub fn bench_bitv_big(b: &mut BenchHarness) {
1472+
let r = rng();
1473+
let mut bitv = Bitv::new(bench_bits, false);
1474+
do b.iter {
1475+
bitv.set((r.next() as uint) % bench_bits, true);
1476+
}
1477+
}
1478+
1479+
#[bench]
1480+
pub fn bench_bitv_small(b: &mut BenchHarness) {
1481+
let r = rng();
1482+
let mut bitv = Bitv::new(uint::bits, false);
1483+
do b.iter {
1484+
bitv.set((r.next() as uint) % uint::bits, true);
1485+
}
1486+
}
1487+
1488+
#[bench]
1489+
pub fn bench_bitv_set_small(b: &mut BenchHarness) {
1490+
let r = rng();
1491+
let mut bitv = BitvSet::new();
1492+
do b.iter {
1493+
bitv.insert((r.next() as uint) % uint::bits);
1494+
}
1495+
}
1496+
1497+
#[bench]
1498+
pub fn bench_bitv_set_big(b: &mut BenchHarness) {
1499+
let r = rng();
1500+
let mut bitv = BitvSet::new();
1501+
do b.iter {
1502+
bitv.insert((r.next() as uint) % bench_bits);
1503+
}
1504+
}
1505+
1506+
#[bench]
1507+
pub fn bench_bitv_big_union(b: &mut BenchHarness) {
1508+
let mut b1 = Bitv::new(bench_bits, false);
1509+
let mut b2 = Bitv::new(bench_bits, false);
1510+
do b.iter {
1511+
b1.union(&b2);
1512+
}
1513+
}
14221514
}
14231515

14241516
//

src/test/bench/core-set.rs

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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

Comments
 (0)