1+ use fxhash:: FxBuildHasher ;
2+ use hashbrown:: HashMap ;
3+
14use benchlib;
25use benchlib:: benchmark:: { black_box, run_benchmark_group} ;
36
7+ fn create_map_1m_integers ( ) -> HashMap < u64 , u64 , FxBuildHasher > {
8+ let mut map: HashMap < u64 , u64 , _ > =
9+ HashMap :: with_capacity_and_hasher ( 1_000_000 , FxBuildHasher :: default ( ) ) ;
10+ for index in 0 ..map. capacity ( ) {
11+ map. insert ( index as u64 , index as u64 ) ;
12+ }
13+ map
14+ }
15+
416fn main ( ) {
17+ let map_1m_integers = create_map_1m_integers ( ) ;
18+
519 run_benchmark_group ( |group| {
620 // Measures how long does it take to insert 1 million numbers into a hashmap.
721 group. register_benchmark ( "hashmap_insert_1m" , || {
822 let count = 1_000_000 ;
9- let mut map = hashbrown :: HashMap :: with_capacity_and_hasher (
23+ let mut map = HashMap :: with_capacity_and_hasher (
1024 // Over allocate the hashmap to avoid reallocations when inserting
1125 count * 2 ,
12- fxhash :: FxBuildHasher :: default ( ) ,
26+ FxBuildHasher :: default ( ) ,
1327 ) ;
1428 move || {
1529 for index in 0 ..count {
@@ -20,66 +34,37 @@ fn main() {
2034
2135 // Measures how long it takes to remove 1 million elements from a hashmap.
2236 group. register_benchmark ( "hashmap_remove_1m" , || {
23- let mut map = hashbrown:: HashMap :: with_capacity_and_hasher (
24- 1_000_000 ,
25- fxhash:: FxBuildHasher :: default ( ) ,
26- ) ;
27- for index in 0 ..map. capacity ( ) {
28- map. insert ( index, index) ;
29- }
30-
37+ let mut map = create_map_1m_integers ( ) ;
3138 move || {
3239 for index in 0 ..map. capacity ( ) {
33- map. remove ( & index) ;
40+ map. remove ( & ( index as u64 ) ) ;
3441 }
3542 }
3643 } ) ;
3744
3845 // Measures how long it takes to find 1 million elements that are in a hashmap.
3946 group. register_benchmark ( "hashmap_find_1m" , || {
40- let mut map = hashbrown:: HashMap :: with_capacity_and_hasher (
41- 1_000_000 ,
42- fxhash:: FxBuildHasher :: default ( ) ,
43- ) ;
44- for index in 0 ..map. capacity ( ) {
45- map. insert ( index, index) ;
46- }
47-
48- move || {
47+ || {
48+ let map = & map_1m_integers;
4949 for index in 0 ..map. capacity ( ) {
50- black_box ( map. get ( & index) ) ;
50+ black_box ( map. get ( & ( index as u64 ) ) ) ;
5151 }
5252 }
5353 } ) ;
5454
5555 // Measures how long it takes to find 1 million elements that are not in a hashmap.
5656 group. register_benchmark ( "hashmap_find_misses_1m" , || {
57- let mut map = hashbrown:: HashMap :: with_capacity_and_hasher (
58- 1_000_000 ,
59- fxhash:: FxBuildHasher :: default ( ) ,
60- ) ;
61- for index in 0 ..map. capacity ( ) {
62- map. insert ( index, index) ;
63- }
64-
65- move || {
57+ || {
58+ let map = & map_1m_integers;
6659 for index in map. capacity ( ) ..( map. capacity ( ) * 2 ) {
67- black_box ( map. get ( & index) ) ;
60+ black_box ( map. get ( & ( index as u64 ) ) ) ;
6861 }
6962 }
7063 } ) ;
7164
7265 // Measures how long it takes to iterate through values of a hashmap with 1 million elements.
7366 group. register_benchmark ( "hashmap_iterate_1m" , || {
74- let mut map = hashbrown:: HashMap :: with_capacity_and_hasher (
75- 1_000_000 ,
76- fxhash:: FxBuildHasher :: default ( ) ,
77- ) ;
78- for index in 0 ..map. capacity ( ) {
79- map. insert ( index, index as u64 ) ;
80- }
81-
82- move || map. values ( ) . sum :: < u64 > ( )
67+ || map_1m_integers. values ( ) . sum :: < u64 > ( )
8368 } ) ;
8469 } ) ;
8570}
0 commit comments