Skip to content

Commit 2d289cc

Browse files
committed
Move benchmark set creation to separate function
To make testing with different elements easier
1 parent 599bb39 commit 2d289cc

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed

benches/set_ops.rs

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,63 +22,68 @@ const SMALL_SET_SIZE: usize = 100;
2222
const OVERLAPP: usize =
2323
[LARGE_SET_SIZE, SMALL_SET_SIZE][(LARGE_SET_SIZE < SMALL_SET_SIZE) as usize] / 2;
2424

25+
/// Creates a set containing end - start unique string elements.
26+
fn create_set(start: usize, end: usize) -> HashSet<String> {
27+
(start..end).map(|nr| format!("key{}", nr)).collect()
28+
}
29+
2530
#[bench]
2631
fn set_ops_bit_or(b: &mut Bencher) {
27-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
28-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
29-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
30-
.map(|nr| format!("key{}", nr))
31-
.collect();
32+
let large_set = create_set(0, LARGE_SET_SIZE);
33+
let small_set = create_set(
34+
LARGE_SET_SIZE - OVERLAPP,
35+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
36+
);
3237
b.iter(|| &large_set | &small_set)
3338
}
3439

3540
#[bench]
3641
fn set_ops_bit_and(b: &mut Bencher) {
37-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
38-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
39-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
40-
.map(|nr| format!("key{}", nr))
41-
.collect();
42+
let large_set = create_set(0, LARGE_SET_SIZE);
43+
let small_set = create_set(
44+
LARGE_SET_SIZE - OVERLAPP,
45+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
46+
);
4247
b.iter(|| &large_set & &small_set)
4348
}
4449

4550
#[bench]
4651
fn set_ops_bit_xor(b: &mut Bencher) {
47-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
48-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
49-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
50-
.map(|nr| format!("key{}", nr))
51-
.collect();
52+
let large_set = create_set(0, LARGE_SET_SIZE);
53+
let small_set = create_set(
54+
LARGE_SET_SIZE - OVERLAPP,
55+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
56+
);
5257
b.iter(|| &large_set ^ &small_set)
5358
}
5459

5560
#[bench]
5661
fn set_ops_sub_large_small(b: &mut Bencher) {
57-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
58-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
59-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
60-
.map(|nr| format!("key{}", nr))
61-
.collect();
62+
let large_set = create_set(0, LARGE_SET_SIZE);
63+
let small_set = create_set(
64+
LARGE_SET_SIZE - OVERLAPP,
65+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
66+
);
6267
b.iter(|| &large_set - &small_set)
6368
}
6469

6570
#[bench]
6671
fn set_ops_sub_small_large(b: &mut Bencher) {
67-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
68-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
69-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
70-
.map(|nr| format!("key{}", nr))
71-
.collect();
72+
let large_set = create_set(0, LARGE_SET_SIZE);
73+
let small_set = create_set(
74+
LARGE_SET_SIZE - OVERLAPP,
75+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
76+
);
7277
b.iter(|| &small_set - &large_set)
7378
}
7479

7580
#[bench]
7681
fn set_ops_bit_or_assign(b: &mut Bencher) {
77-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
78-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
79-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
80-
.map(|nr| format!("key{}", nr))
81-
.collect();
82+
let large_set = create_set(0, LARGE_SET_SIZE);
83+
let small_set = create_set(
84+
LARGE_SET_SIZE - OVERLAPP,
85+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
86+
);
8287
b.iter(|| {
8388
let mut set = large_set.clone();
8489
set |= &small_set;
@@ -88,11 +93,11 @@ fn set_ops_bit_or_assign(b: &mut Bencher) {
8893

8994
#[bench]
9095
fn set_ops_bit_and_assign(b: &mut Bencher) {
91-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
92-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
93-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
94-
.map(|nr| format!("key{}", nr))
95-
.collect();
96+
let large_set = create_set(0, LARGE_SET_SIZE);
97+
let small_set = create_set(
98+
LARGE_SET_SIZE - OVERLAPP,
99+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
100+
);
96101
b.iter(|| {
97102
let mut set = small_set.clone();
98103
set &= &large_set;
@@ -102,11 +107,11 @@ fn set_ops_bit_and_assign(b: &mut Bencher) {
102107

103108
#[bench]
104109
fn set_ops_bit_xor_assign(b: &mut Bencher) {
105-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
106-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
107-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
108-
.map(|nr| format!("key{}", nr))
109-
.collect();
110+
let large_set = create_set(0, LARGE_SET_SIZE);
111+
let small_set = create_set(
112+
LARGE_SET_SIZE - OVERLAPP,
113+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
114+
);
110115
b.iter(|| {
111116
let mut set = large_set.clone();
112117
set ^= &small_set;
@@ -116,11 +121,11 @@ fn set_ops_bit_xor_assign(b: &mut Bencher) {
116121

117122
#[bench]
118123
fn set_ops_sub_assign_large_small(b: &mut Bencher) {
119-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
120-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
121-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
122-
.map(|nr| format!("key{}", nr))
123-
.collect();
124+
let large_set = create_set(0, LARGE_SET_SIZE);
125+
let small_set = create_set(
126+
LARGE_SET_SIZE - OVERLAPP,
127+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
128+
);
124129
b.iter(|| {
125130
let mut set = large_set.clone();
126131
set -= &small_set;
@@ -130,11 +135,11 @@ fn set_ops_sub_assign_large_small(b: &mut Bencher) {
130135

131136
#[bench]
132137
fn set_ops_sub_assign_small_large(b: &mut Bencher) {
133-
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect();
134-
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP)
135-
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP))
136-
.map(|nr| format!("key{}", nr))
137-
.collect();
138+
let large_set = create_set(0, LARGE_SET_SIZE);
139+
let small_set = create_set(
140+
LARGE_SET_SIZE - OVERLAPP,
141+
LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP,
142+
);
138143
b.iter(|| {
139144
let mut set = small_set.clone();
140145
set -= &large_set;

0 commit comments

Comments
 (0)