From 4decb58a64fd3d0a0da5cbea4a77327996cbe44a Mon Sep 17 00:00:00 2001 From: Stein Somers Date: Thu, 14 Nov 2019 00:32:36 +0100 Subject: [PATCH] introduce benchmarks of HashSet operations --- src/liballoc/benches/btree/set.rs | 8 +--- .../hash/bench.rs => benches/hash/map.rs} | 15 +------ src/libstd/benches/hash/mod.rs | 2 + src/libstd/benches/hash/set_ops.rs | 42 +++++++++++++++++++ src/libstd/benches/lib.rs | 5 +++ src/libstd/collections/hash/mod.rs | 1 - 6 files changed, 52 insertions(+), 21 deletions(-) rename src/libstd/{collections/hash/bench.rs => benches/hash/map.rs} (88%) create mode 100644 src/libstd/benches/hash/mod.rs create mode 100644 src/libstd/benches/hash/set_ops.rs create mode 100644 src/libstd/benches/lib.rs diff --git a/src/liballoc/benches/btree/set.rs b/src/liballoc/benches/btree/set.rs index 6357ea3ea11bd..18502ded308c7 100644 --- a/src/liballoc/benches/btree/set.rs +++ b/src/liballoc/benches/btree/set.rs @@ -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 { let mut rng = thread_rng(); @@ -31,7 +31,6 @@ fn pos(n: usize) -> BTreeSet { set } - fn stagger(n1: usize, factor: usize) -> [BTreeSet; 2] { let n2 = n1 * factor; let mut sets = [BTreeSet::new(), BTreeSet::new()]; @@ -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()) } }; } diff --git a/src/libstd/collections/hash/bench.rs b/src/libstd/benches/hash/map.rs similarity index 88% rename from src/libstd/collections/hash/bench.rs rename to src/libstd/benches/hash/map.rs index 7f5add0543685..25425c5968e25 100644 --- a/src/libstd/collections/hash/bench.rs +++ b/src/libstd/benches/hash/map.rs @@ -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 = HashMap::new(); assert_eq!(m.len(), 0); @@ -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); @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/src/libstd/benches/hash/mod.rs b/src/libstd/benches/hash/mod.rs new file mode 100644 index 0000000000000..42401a21b766d --- /dev/null +++ b/src/libstd/benches/hash/mod.rs @@ -0,0 +1,2 @@ +mod map; +mod set_ops; diff --git a/src/libstd/benches/hash/set_ops.rs b/src/libstd/benches/hash/set_ops.rs new file mode 100644 index 0000000000000..1a4c4a66ee9e0 --- /dev/null +++ b/src/libstd/benches/hash/set_ops.rs @@ -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()); +} diff --git a/src/libstd/benches/lib.rs b/src/libstd/benches/lib.rs new file mode 100644 index 0000000000000..4d1cf7fab7b7b --- /dev/null +++ b/src/libstd/benches/lib.rs @@ -0,0 +1,5 @@ +#![feature(test)] + +extern crate test; + +mod hash; diff --git a/src/libstd/collections/hash/mod.rs b/src/libstd/collections/hash/mod.rs index a6d89a4d32abf..348820af54bff 100644 --- a/src/libstd/collections/hash/mod.rs +++ b/src/libstd/collections/hash/mod.rs @@ -1,5 +1,4 @@ //! Unordered containers, implemented as hash-tables -mod bench; pub mod map; pub mod set;