diff --git a/CMakeLists.txt b/CMakeLists.txt index 15017bc..9fbf47f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -129,7 +129,7 @@ if (PHMAP_BUILD_EXAMPLES) add_executable(ex_basic examples/basic.cc phmap.natvis) add_executable(ex_bench examples/bench.cc phmap.natvis) add_executable(ex_emplace examples/emplace.cc phmap.natvis) - # add_executable(ex_serialize examples/serialize.cc phmap.natvis) + add_executable(ex_serialize examples/serialize.cc phmap.natvis) add_executable(ex_hash_std examples/hash_std.cc phmap.natvis) add_executable(ex_hash_value examples/hash_value.cc phmap.natvis) add_executable(ex_two_files examples/f1.cc examples/f2.cc phmap.natvis) diff --git a/examples/serialize.cc b/examples/serialize.cc index a9f33f8..d6b836f 100644 --- a/examples/serialize.cc +++ b/examples/serialize.cc @@ -12,7 +12,6 @@ #include "cereal/archives/binary.hpp" #include #endif -#include #include #include #include @@ -21,6 +20,39 @@ using phmap::flat_hash_map; using namespace std; template using milliseconds = std::chrono::duration; +// -------------------------------------------------------------------------- +// from: https://github.com/preshing/RandomSequence +// -------------------------------------------------------------------------- +class RSU +{ +private: + unsigned int m_index; + unsigned int m_intermediateOffset; + + static unsigned int permuteQPR(unsigned int x) + { + static const unsigned int prime = 4294967291u; + if (x >= prime) + return x; // The 5 integers out of range are mapped to themselves. + unsigned int residue = ((unsigned long long) x * x) % prime; + return (x <= prime / 2) ? residue : prime - residue; + } + +public: + RSU(unsigned int seedBase, unsigned int seedOffset) + { + m_index = permuteQPR(permuteQPR(seedBase) + 0x682f0161); + m_intermediateOffset = permuteQPR(permuteQPR(seedOffset) + 0x46790905); + } + + unsigned int next() + { + return permuteQPR((permuteQPR(m_index++) + m_intermediateOffset) ^ 0x5bf03635); + } +}; + +// -------------------------------------------------------------------------- +// -------------------------------------------------------------------------- void showtime(const char *name, std::function doit) { auto t1 = std::chrono::high_resolution_clock::now(); @@ -30,26 +62,23 @@ void showtime(const char *name, std::function doit) printf("%s: %.3fs\n", name, (int)elapsed / 1000.0f); } +// -------------------------------------------------------------------------- +// -------------------------------------------------------------------------- int main() { - using MapType = phmap::flat_hash_map, int>; + using MapType = phmap::flat_hash_map; MapType table; const int num_items = 100000000; // Iterate and add keys and values // ------------------------------- showtime("build hash", [&table, num_items]() { - random_device dev; - mt19937 rng(dev()); - uniform_int_distribution dist6(0,1); + unsigned int seed = 76687; + RSU rsu(seed, seed + 1); + table.reserve(num_items); for (int i=0; i < num_items; ++i) - { - bitset<42> bs; - for (int j=0; j<42; ++j) - bs[j] = dist6(rng); - table[bs] = i; - } + table.insert(typename MapType::value_type(rsu.next(), i)); }); // cerealize and save data