@@ -348,14 +348,17 @@ void associative_container_benchmarks(std::string container) {
348348
349349 bench (" erase(key) (non-existent)" , [=](auto & st) {
350350 const std::size_t size = st.range (0 );
351- std::vector<Value> in = make_value_types (generate_unique_keys (size + 1 ));
352- Value element = in.back ();
353- in.pop_back ();
351+ std::vector<Value> in = make_value_types (generate_unique_keys (size + BatchSize));
352+ std::vector<Key> keys;
353+ for (std::size_t i = 0 ; i != BatchSize; ++i) {
354+ keys.push_back (get_key (in.back ()));
355+ in.pop_back ();
356+ }
354357 Container c (in.begin (), in.end ());
355358
356359 while (st.KeepRunningBatch (BatchSize)) {
357360 for (std::size_t i = 0 ; i != BatchSize; ++i) {
358- auto result = c.erase (get_key (element) );
361+ auto result = c.erase (keys[i] );
359362 benchmark::DoNotOptimize (result);
360363 benchmark::DoNotOptimize (c);
361364 benchmark::ClobberMemory ();
@@ -449,16 +452,20 @@ void associative_container_benchmarks(std::string container) {
449452 // ///////////////////////
450453 // Query
451454 // ///////////////////////
452- auto bench_with_existent_key = [=](auto func) {
455+ auto with_existent_key = [=](auto func) {
453456 return [=](auto & st) {
454457 const std::size_t size = st.range (0 );
455458 std::vector<Value> in = make_value_types (generate_unique_keys (size));
456- Value element = in[in.size () / 2 ]; // pick any element
459+ // Pick any `BatchSize` number of elements
460+ std::vector<Key> keys;
461+ for (std::size_t i = 0 ; i < in.size (); i += (in.size () / BatchSize)) {
462+ keys.push_back (get_key (in.at (i)));
463+ }
457464 Container c (in.begin (), in.end ());
458465
459466 while (st.KeepRunningBatch (BatchSize)) {
460467 for (std::size_t i = 0 ; i != BatchSize; ++i) {
461- auto result = func (c, element );
468+ auto result = func (c, keys[i] );
462469 benchmark::DoNotOptimize (c);
463470 benchmark::DoNotOptimize (result);
464471 benchmark::ClobberMemory ();
@@ -467,17 +474,20 @@ void associative_container_benchmarks(std::string container) {
467474 };
468475 };
469476
470- auto bench_with_nonexistent_key = [=](auto func) {
477+ auto with_nonexistent_key = [=](auto func) {
471478 return [=](auto & st) {
472479 const std::size_t size = st.range (0 );
473- std::vector<Value> in = make_value_types (generate_unique_keys (size + 1 ));
474- Value element = in.back ();
475- in.pop_back ();
480+ std::vector<Value> in = make_value_types (generate_unique_keys (size + BatchSize));
481+ std::vector<Key> keys;
482+ for (std::size_t i = 0 ; i != BatchSize; ++i) {
483+ keys.push_back (get_key (in.back ()));
484+ in.pop_back ();
485+ }
476486 Container c (in.begin (), in.end ());
477487
478488 while (st.KeepRunningBatch (BatchSize)) {
479489 for (std::size_t i = 0 ; i != BatchSize; ++i) {
480- auto result = func (c, element );
490+ auto result = func (c, keys[i] );
481491 benchmark::DoNotOptimize (c);
482492 benchmark::DoNotOptimize (result);
483493 benchmark::ClobberMemory ();
@@ -486,45 +496,30 @@ void associative_container_benchmarks(std::string container) {
486496 };
487497 };
488498
489- bench (" find(key) (existent)" ,
490- bench_with_existent_key ([=](Container const & c, Value const & element) { return c.find (get_key (element)); }));
491- bench (" find(key) (non-existent)" ,
492- bench_with_nonexistent_key ([=](Container const & c, Value const & element) { return c.find (get_key (element)); }));
499+ auto find = [](Container const & c, Key const & key) { return c.find (key); };
500+ bench (" find(key) (existent)" , with_existent_key (find));
501+ bench (" find(key) (non-existent)" , with_nonexistent_key (find));
493502
494- bench (" count(key) (existent)" ,
495- bench_with_existent_key ([=](Container const & c, Value const & element) { return c.count (get_key (element)); }));
496- bench (" count(key) (non-existent)" , bench_with_nonexistent_key ([=](Container const & c, Value const & element) {
497- return c.count (get_key (element));
498- }));
503+ auto count = [](Container const & c, Key const & key) { return c.count (key); };
504+ bench (" count(key) (existent)" , with_existent_key (count));
505+ bench (" count(key) (non-existent)" , with_nonexistent_key (count));
499506
500- bench (" contains(key) (existent)" , bench_with_existent_key ([=](Container const & c, Value const & element) {
501- return c.contains (get_key (element));
502- }));
503- bench (" contains(key) (non-existent)" , bench_with_nonexistent_key ([=](Container const & c, Value const & element) {
504- return c.contains (get_key (element));
505- }));
507+ auto contains = [](Container const & c, Key const & key) { return c.contains (key); };
508+ bench (" contains(key) (existent)" , with_existent_key (contains));
509+ bench (" contains(key) (non-existent)" , with_nonexistent_key (contains));
506510
507511 if constexpr (is_ordered_container) {
508- bench (" lower_bound(key) (existent)" , bench_with_existent_key ([=](Container const & c, Value const & element) {
509- return c.lower_bound (get_key (element));
510- }));
511- bench (" lower_bound(key) (non-existent)" , bench_with_nonexistent_key ([=](Container const & c, Value const & element) {
512- return c.lower_bound (get_key (element));
513- }));
514-
515- bench (" upper_bound(key) (existent)" , bench_with_existent_key ([=](Container const & c, Value const & element) {
516- return c.upper_bound (get_key (element));
517- }));
518- bench (" upper_bound(key) (non-existent)" , bench_with_nonexistent_key ([=](Container const & c, Value const & element) {
519- return c.upper_bound (get_key (element));
520- }));
521-
522- bench (" equal_range(key) (existent)" , bench_with_existent_key ([=](Container const & c, Value const & element) {
523- return c.equal_range (get_key (element));
524- }));
525- bench (" equal_range(key) (non-existent)" , bench_with_nonexistent_key ([=](Container const & c, Value const & element) {
526- return c.equal_range (get_key (element));
527- }));
512+ auto lower_bound = [](Container const & c, Key const & key) { return c.lower_bound (key); };
513+ bench (" lower_bound(key) (existent)" , with_existent_key (lower_bound));
514+ bench (" lower_bound(key) (non-existent)" , with_nonexistent_key (lower_bound));
515+
516+ auto upper_bound = [](Container const & c, Key const & key) { return c.upper_bound (key); };
517+ bench (" upper_bound(key) (existent)" , with_existent_key (upper_bound));
518+ bench (" upper_bound(key) (non-existent)" , with_nonexistent_key (upper_bound));
519+
520+ auto equal_range = [](Container const & c, Key const & key) { return c.equal_range (key); };
521+ bench (" equal_range(key) (existent)" , with_existent_key (equal_range));
522+ bench (" equal_range(key) (non-existent)" , with_nonexistent_key (equal_range));
528523 }
529524}
530525
0 commit comments