From 1cb0908961317aa18558be576bd1ea48a174924b Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 20 Jan 2022 02:06:00 +0200 Subject: [PATCH] Change random indices in string.cpp to differ in size; remove shifted consecutive there as not representative --- benchmark/string.cpp | 47 ++++++++++---------------------------------- 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/benchmark/string.cpp b/benchmark/string.cpp index 6ce5b775..0e49e47b 100644 --- a/benchmark/string.cpp +++ b/benchmark/string.cpp @@ -33,7 +33,7 @@ static void print_time( std::chrono::steady_clock::time_point & t1, char const* constexpr unsigned N = 2'000'000; constexpr int K = 10; -static std::vector indices1, indices2, indices3; +static std::vector indices1, indices2; static std::string make_index( unsigned x ) { @@ -43,6 +43,14 @@ static std::string make_index( unsigned x ) return buffer; } +static std::string make_random_index( unsigned x ) +{ + char buffer[ 64 ]; + std::snprintf( buffer, sizeof(buffer), "pfx_%0*d_%u_sfx", x % 8 + 1, 0, x ); + + return buffer; +} + static void init_indices() { indices1.reserve( N*2+1 ); @@ -61,17 +69,9 @@ static void init_indices() for( unsigned i = 1; i <= N*2; ++i ) { - indices2.push_back( make_index( static_cast( rng() ) ) ); + indices2.push_back( make_random_index( static_cast( rng() ) ) ); } } - - indices3.reserve( N*2+1 ); - indices3.push_back( make_index( 0 ) ); - - for( unsigned i = 1; i <= N*2; ++i ) - { - indices3.push_back( make_index( (std::uint32_t)i << 11 ) ); - } } template void test_insert( Map& map, std::chrono::steady_clock::time_point & t1 ) @@ -90,13 +90,6 @@ template void test_insert( Map& map, std::chrono::steady_clock::time_ print_time( t1, "Random insert", 0, map.size() ); - for( unsigned i = 1; i <= N; ++i ) - { - map.insert( { indices3[ i ], i } ); - } - - print_time( t1, "Consecutive shifted insert", 0, map.size() ); - std::cout << std::endl; } @@ -130,19 +123,6 @@ template void test_lookup( Map& map, std::chrono::steady_clock::time_ print_time( t1, "Random lookup", s, map.size() ); - s = 0; - - for( int j = 0; j < K; ++j ) - { - for( unsigned i = 1; i <= N * 2; ++i ) - { - auto it = map.find( indices3[ i ] ); - if( it != map.end() ) s += it->second; - } - } - - print_time( t1, "Consecutive shifted lookup", s, map.size() ); - std::cout << std::endl; } @@ -187,13 +167,6 @@ template void test_erase( Map& map, std::chrono::steady_clock::time_p print_time( t1, "Random erase", 0, map.size() ); - for( unsigned i = 1; i <= N; ++i ) - { - map.erase( indices3[ i ] ); - } - - print_time( t1, "Consecutive shifted erase", 0, map.size() ); - std::cout << std::endl; }