See More

// Copyright 2021 Peter Dimov. // Copyright 2023-2024 Joaquin M Lopez Munoz. // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #define _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING #define _SILENCE_CXX20_CISO646_REMOVED_WARNING #define BOOST_UNORDERED_ENABLE_STATS #include #include #include #include #include #include #include #include #include #include #include using namespace std::chrono_literals; static void print_time( std::chrono::steady_clock::time_point & t1, char const* label, std::uint32_t s, std::size_t size ) { auto t2 = std::chrono::steady_clock::now(); std::cout << label << ": " << ( t2 - t1 ) / 1ms << " ms (s=" << s << ", size=" << size << ")\n"; t1 = t2; } constexpr unsigned N = 50'000; constexpr int K = 10; static std::vector<:string> indices1, indices2; static std::string make_index( unsigned x ) { char buffer[ 64 ]; std::snprintf( buffer, sizeof(buffer), "pfx_%u_sfx", 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 ); indices1.push_back( make_index( 0 ) ); for( unsigned i = 1; i <= N*2; ++i ) { indices1.push_back( make_index( i ) ); } indices2.reserve( N*2+1 ); indices2.push_back( make_index( 0 ) ); { boost::detail::splitmix64 rng; for( unsigned i = 1; i <= N*2; ++i ) { indices2.push_back( make_random_index( static_cast<:uint32_t>( rng() ) ) ); } } } template BOOST_NOINLINE void test_insert( Map& map, std::chrono::steady_clock::time_point & t1 ) { for( unsigned i = 1; i <= N; ++i ) { map.insert( { indices1[ i ], i } ); } print_time( t1, "Consecutive insert", 0, map.size() ); for( unsigned i = 1; i <= N; ++i ) { map.insert( { indices2[ i ], i } ); } print_time( t1, "Random insert", 0, map.size() ); std::cout << std::endl; } template BOOST_NOINLINE void test_lookup( Map& map, std::chrono::steady_clock::time_point & t1 ) { std::uint32_t s; s = 0; for( int j = 0; j < K; ++j ) { for( unsigned i = 1; i <= N * 2; ++i ) { auto it = map.find( indices1[ i ] ); if( it != map.end() ) s += it->second; } } print_time( t1, "Consecutive 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( indices2[ i ] ); if( it != map.end() ) s += it->second; } } print_time( t1, "Random lookup", s, map.size() ); std::cout << std::endl; } template BOOST_NOINLINE void test_iteration( Map& map, std::chrono::steady_clock::time_point & t1 ) { auto it = map.begin(); while( it != map.end() ) { if( it->second & 1 ) { if constexpr( std::is_void_v< decltype( map.erase( it ) ) > ) { map.erase( it++ ); } else { it = map.erase( it ); } } else { ++it; } } print_time( t1, "Iterate and erase odd elements", 0, map.size() ); std::cout << std::endl; } template BOOST_NOINLINE void test_erase( Map& map, std::chrono::steady_clock::time_point & t1 ) { for( unsigned i = 1; i <= N; ++i ) { map.erase( indices1[ i ] ); } print_time( t1, "Consecutive erase", 0, map.size() ); for( unsigned i = 1; i <= N; ++i ) { map.erase( indices2[ i ] ); } print_time( t1, "Random erase", 0, map.size() ); std::cout << std::endl; } // // All Unordered container use the same struct using stats = boost::unordered_flat_map::stats; struct record { std::string label_; long long time_; stats stats_; }; static std::vector records; template