simplify serialize.cc, add it to the examples in CMakeLists.txt

This commit is contained in:
greg
2019-09-08 11:12:12 -04:00
parent 289fadc273
commit 8834c9d624
2 changed files with 41 additions and 12 deletions
+1 -1
View File
@@ -129,7 +129,7 @@ if (PHMAP_BUILD_EXAMPLES)
add_executable(ex_basic examples/basic.cc phmap.natvis) add_executable(ex_basic examples/basic.cc phmap.natvis)
add_executable(ex_bench examples/bench.cc phmap.natvis) add_executable(ex_bench examples/bench.cc phmap.natvis)
add_executable(ex_emplace examples/emplace.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_std examples/hash_std.cc phmap.natvis)
add_executable(ex_hash_value examples/hash_value.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) add_executable(ex_two_files examples/f1.cc examples/f2.cc phmap.natvis)
+40 -11
View File
@@ -12,7 +12,6 @@
#include "cereal/archives/binary.hpp" #include "cereal/archives/binary.hpp"
#include <fstream> #include <fstream>
#endif #endif
#include <random>
#include <chrono> #include <chrono>
#include <functional> #include <functional>
#include <cstdio> #include <cstdio>
@@ -21,6 +20,39 @@ using phmap::flat_hash_map;
using namespace std; using namespace std;
template <typename T> using milliseconds = std::chrono::duration<T, std::milli>; template <typename T> using milliseconds = std::chrono::duration<T, std::milli>;
// --------------------------------------------------------------------------
// 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<void ()> doit) void showtime(const char *name, std::function<void ()> doit)
{ {
auto t1 = std::chrono::high_resolution_clock::now(); auto t1 = std::chrono::high_resolution_clock::now();
@@ -30,26 +62,23 @@ void showtime(const char *name, std::function<void ()> doit)
printf("%s: %.3fs\n", name, (int)elapsed / 1000.0f); printf("%s: %.3fs\n", name, (int)elapsed / 1000.0f);
} }
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
int main() int main()
{ {
using MapType = phmap::flat_hash_map<bitset<42>, int>; using MapType = phmap::flat_hash_map<unsigned int, int>;
MapType table; MapType table;
const int num_items = 100000000; const int num_items = 100000000;
// Iterate and add keys and values // Iterate and add keys and values
// ------------------------------- // -------------------------------
showtime("build hash", [&table, num_items]() { showtime("build hash", [&table, num_items]() {
random_device dev; unsigned int seed = 76687;
mt19937 rng(dev()); RSU rsu(seed, seed + 1);
uniform_int_distribution<mt19937::result_type> dist6(0,1);
table.reserve(num_items); table.reserve(num_items);
for (int i=0; i < num_items; ++i) for (int i=0; i < num_items; ++i)
{ table.insert(typename MapType::value_type(rsu.next(), i));
bitset<42> bs;
for (int j=0; j<42; ++j)
bs[j] = dist6(rng);
table[bs] = i;
}
}); });
// cerealize and save data // cerealize and save data