-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement XxxAssign operations for HashSets
Also add a set of benchmarks for set operations
- Loading branch information
Showing
2 changed files
with
334 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
//! This file contains benchmarks for the ops traits implemented by HashSet. | ||
//! Each test is intended to have a defined larger and smaller set, | ||
//! but using a larger size for the "small" set works just as well. | ||
//! | ||
//! Each assigning test is done in the configuration that is faster. Cheating, I know. | ||
//! The exception to this is Sub, because there the result differs. So I made two benchmarks for Sub. | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
|
||
use hashbrown::HashSet; | ||
use test::Bencher; | ||
|
||
/// The number of items to generate for the larger of the sets. | ||
const LARGE_SET_SIZE: usize = 1000; | ||
|
||
/// The number of items to generate for the smaller of the sets. | ||
const SMALL_SET_SIZE: usize = 100; | ||
|
||
/// The number of keys present in both sets. | ||
const OVERLAPP: usize = | ||
[LARGE_SET_SIZE, SMALL_SET_SIZE][(LARGE_SET_SIZE < SMALL_SET_SIZE) as usize] / 2; | ||
|
||
#[bench] | ||
fn set_ops_bit_or(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| &large_set | &small_set) | ||
} | ||
|
||
#[bench] | ||
fn set_ops_bit_and(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| &large_set & &small_set) | ||
} | ||
|
||
#[bench] | ||
fn set_ops_bit_xor(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| &large_set ^ &small_set) | ||
} | ||
|
||
#[bench] | ||
fn set_ops_add(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| &large_set + &small_set) | ||
} | ||
|
||
#[bench] | ||
fn set_ops_sub_large_small(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| &large_set - &small_set) | ||
} | ||
|
||
#[bench] | ||
fn set_ops_sub_small_large(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| &small_set - &large_set) | ||
} | ||
|
||
#[bench] | ||
fn set_ops_bit_or_assign(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| { | ||
let mut set = large_set.clone(); | ||
set |= &small_set; | ||
set | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn set_ops_bit_and_assign(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| { | ||
let mut set = small_set.clone(); | ||
set &= &large_set; | ||
set | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn set_ops_bit_xor_assign(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| { | ||
let mut set = large_set.clone(); | ||
set ^= &small_set; | ||
set | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn set_ops_add_assign(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| { | ||
let mut set = large_set.clone(); | ||
set += &small_set; | ||
set | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn set_ops_sub_assign_large_small(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| { | ||
let mut set = large_set.clone(); | ||
set -= &small_set; | ||
set | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn set_ops_sub_assign_small_large(b: &mut Bencher) { | ||
let large_set: HashSet<_> = (0..LARGE_SET_SIZE).map(|nr| format!("key{}", nr)).collect(); | ||
let small_set: HashSet<_> = ((LARGE_SET_SIZE - OVERLAPP) | ||
..(LARGE_SET_SIZE + SMALL_SET_SIZE - OVERLAPP)) | ||
.map(|nr| format!("key{}", nr)) | ||
.collect(); | ||
b.iter(|| { | ||
let mut set = small_set.clone(); | ||
set -= &large_set; | ||
set | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters