// Copyright 2017-2020 Peter Dimov. // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) template class hasher { private: H h_; private: template static void hash_append_impl( H& h, T const& v, std::false_type ) { boost::hash2::hash_append( h, {}, v ); } template static void hash_append_impl( H& h, T const& v, std::true_type ) { boost::hash2::hash_append_range( h, {}, v.data(), v.data() + v.size() ); } public: hasher(): h_() { } explicit hasher( std::uint64_t seed ): h_( seed ) { } hasher( unsigned char const* seed, std::size_t n ): h_( seed, n ) { } std::size_t operator()( T const& v ) const { H h( h_ ); hash_append_impl( h, v, boost::container_hash::is_contiguous_range() ); return boost::hash2::get_integral_result( h ); } }; template void test3( int N, V const& v, std::size_t seed ) { typedef std::chrono::steady_clock clock_type; clock_type::time_point t1 = clock_type::now(); std::size_t q = 0; hasher const h( seed ); for( int i = 0; i < N; ++i ) { q += h( v[i] ); } clock_type::time_point t2 = clock_type::now(); long long ms1 = std::chrono::duration_cast( t2 - t1 ).count(); std::string hash = boost::core::type_name(); std::printf( "%s: q=%zu, %lld ms\n", hash.c_str(), q, ms1 ); } template void test2( int N, V const& v ) { test3( N, v, 0x9e3779b9 ); } int main() { int const N = 16 * 1048576; std::vector v; { v.reserve( N ); std::mt19937_64 rnd; for( int i = 0; i < N; ++i ) { char buffer[ 64 ]; unsigned long long k = rnd(); if( k & 1 ) { std::snprintf( buffer, sizeof( buffer ), "prefix_%llu_suffix", k ); } else { std::snprintf( buffer, sizeof( buffer ), "{%u}", static_cast( k ) ); } v.push_back( buffer ); } } using namespace boost::hash2; test2( N, v ); test2( N, v ); test2( N, v ); test2( N, v ); test2( N, v ); test2( N, v ); test2( N, v ); std::puts( "" ); }