Skip to content

Commit

Permalink
Auto merge of #66393 - ssomers:hash_benches, r=dtolnay
Browse files Browse the repository at this point in the history
introduce benchmarks of HashSet operations

To avoid goofs such as corrected by #66280, I added benchmarks of binary HashSet operations.

Due to the fact x.py keeps recompiling the whole shebang (or at least a big part of it) whenever you touch the test code, and because piling up all tests in one file does not strike me as future proof, I tried moving the hash benches to the separate place they are for liballoc/collections/btree. But it turns out that, in a cleaned checkout, x.py still recompiles the whole shebang whenever you touch the test code (PS or when you add or delete any irrelevant file). So I'm not going to add more tests, and I doubt others will, and these tests have proven their point already, so this PR is kind of pointless
  • Loading branch information
bors committed Nov 24, 2019
2 parents 7d761fe + 4decb58 commit b56b239
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
8 changes: 2 additions & 6 deletions src/liballoc/benches/btree/set.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeSet;

use rand::{thread_rng, Rng};
use test::{black_box, Bencher};
use test::Bencher;

fn random(n: usize) -> BTreeSet<usize> {
let mut rng = thread_rng();
Expand Down Expand Up @@ -31,7 +31,6 @@ fn pos(n: usize) -> BTreeSet<i32> {
set
}


fn stagger(n1: usize, factor: usize) -> [BTreeSet<u32>; 2] {
let n2 = n1 * factor;
let mut sets = [BTreeSet::new(), BTreeSet::new()];
Expand All @@ -52,10 +51,7 @@ macro_rules! set_bench {
let sets = $sets;

// measure
b.iter(|| {
let x = sets[0].$set_func(&sets[1]).$result_func();
black_box(x);
})
b.iter(|| sets[0].$set_func(&sets[1]).$result_func())
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![cfg(test)]

use test::Bencher;
use std::collections::HashMap;

#[bench]
fn new_drop(b: &mut Bencher) {
use super::map::HashMap;

b.iter(|| {
let m: HashMap<i32, i32> = HashMap::new();
assert_eq!(m.len(), 0);
Expand All @@ -14,8 +13,6 @@ fn new_drop(b: &mut Bencher) {

#[bench]
fn new_insert_drop(b: &mut Bencher) {
use super::map::HashMap;

b.iter(|| {
let mut m = HashMap::new();
m.insert(0, 0);
Expand All @@ -25,8 +22,6 @@ fn new_insert_drop(b: &mut Bencher) {

#[bench]
fn grow_by_insertion(b: &mut Bencher) {
use super::map::HashMap;

let mut m = HashMap::new();

for i in 1..1001 {
Expand All @@ -43,8 +38,6 @@ fn grow_by_insertion(b: &mut Bencher) {

#[bench]
fn find_existing(b: &mut Bencher) {
use super::map::HashMap;

let mut m = HashMap::new();

for i in 1..1001 {
Expand All @@ -60,8 +53,6 @@ fn find_existing(b: &mut Bencher) {

#[bench]
fn find_nonexisting(b: &mut Bencher) {
use super::map::HashMap;

let mut m = HashMap::new();

for i in 1..1001 {
Expand All @@ -77,8 +68,6 @@ fn find_nonexisting(b: &mut Bencher) {

#[bench]
fn hashmap_as_queue(b: &mut Bencher) {
use super::map::HashMap;

let mut m = HashMap::new();

for i in 1..1001 {
Expand All @@ -96,8 +85,6 @@ fn hashmap_as_queue(b: &mut Bencher) {

#[bench]
fn get_remove_insert(b: &mut Bencher) {
use super::map::HashMap;

let mut m = HashMap::new();

for i in 1..1001 {
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/benches/hash/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod map;
mod set_ops;
42 changes: 42 additions & 0 deletions src/libstd/benches/hash/set_ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::collections::HashSet;
use test::Bencher;

#[bench]
fn set_difference(b: &mut Bencher) {
let small: HashSet<_> = (0..10).collect();
let large: HashSet<_> = (0..100).collect();

b.iter(|| small.difference(&large).count());
}

#[bench]
fn set_is_subset(b: &mut Bencher) {
let small: HashSet<_> = (0..10).collect();
let large: HashSet<_> = (0..100).collect();

b.iter(|| small.is_subset(&large));
}

#[bench]
fn set_intersection(b: &mut Bencher) {
let small: HashSet<_> = (0..10).collect();
let large: HashSet<_> = (0..100).collect();

b.iter(|| small.intersection(&large).count());
}

#[bench]
fn set_symmetric_difference(b: &mut Bencher) {
let small: HashSet<_> = (0..10).collect();
let large: HashSet<_> = (0..100).collect();

b.iter(|| small.symmetric_difference(&large).count());
}

#[bench]
fn set_union(b: &mut Bencher) {
let small: HashSet<_> = (0..10).collect();
let large: HashSet<_> = (0..100).collect();

b.iter(|| small.union(&large).count());
}
5 changes: 5 additions & 0 deletions src/libstd/benches/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![feature(test)]

extern crate test;

mod hash;
1 change: 0 additions & 1 deletion src/libstd/collections/hash/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Unordered containers, implemented as hash-tables

mod bench;
pub mod map;
pub mod set;

0 comments on commit b56b239

Please sign in to comment.