From a381f47b15970c54aceec589251daf760544a31a Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 10 Apr 2019 08:12:25 -0400 Subject: [PATCH] improve emplace.cc example. --- examples/emplace.cc | 99 +++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/examples/emplace.cc b/examples/emplace.cc index c97a9bb..540318c 100644 --- a/examples/emplace.cc +++ b/examples/emplace.cc @@ -8,19 +8,11 @@ #include -namespace patch -{ - template std::string to_string(const T& n) - { - std::ostringstream stm; - stm << n; - return stm.str(); - } -} - template using milliseconds = std::chrono::duration; +// type containing std::string. Seems to take a long time to construct (and maybe move) +// ------------------------------------------------------------------------------------ class custom_type { std::string one = "one"; @@ -40,6 +32,8 @@ public: //custom_type& operator=(custom_type const&) = delete; }; +// type containing only integrals. should be faster to create. +// ----------------------------------------------------------- class custom_type_2 { std::uint32_t three = 3; @@ -58,64 +52,65 @@ public: //custom_type_2& operator=(custom_type_2 const&) = delete; }; -// emplace string + large struct -// ----------------------------- -template struct _emplace +// convert std::size_t to appropriate key +// -------------------------------------- +template +struct GenKey +{ + K operator()(std::size_t j); +}; + +template <> +struct GenKey +{ + std::string operator()(std::size_t j) { + std::ostringstream stm; + stm << j; + return stm.str(); + } +}; + +template <> +struct GenKey +{ + int operator()(std::size_t j) { + return (int)j; + } +}; + +// emplace key + large struct +// -------------------------- +template struct _emplace { void operator()(Map &m, std::size_t j); }; // "void" template parameter -> use emplace -template struct _emplace +template struct _emplace { void operator()(Map &m, std::size_t j) { - m.emplace(patch::to_string(j), V()); + m.emplace(GenKey()(j), V()); } }; // "int" template parameter -> use emplace_back for std::vector -template struct _emplace +template struct _emplace { void operator()(Map &m, std::size_t j) { - m.emplace_back(patch::to_string(j), V()); - } -}; - -// insert -// --------------------------- -template struct emplace_2_ints -{ - void operator()(Map &m, std::size_t j); -}; - -// "void" template parameter -> use emplace -template struct emplace_2_ints -{ - void operator()(Map &m, std::size_t j) - { - m.emplace(j, V()); - } -}; - -// "int" template parameter -> use emplace_back for std::vector -template struct emplace_2_ints -{ - void operator()(Map &m, std::size_t j) - { - m.emplace_back(j, V()); + m.emplace_back(GenKey()(j), V()); } }; // The test itself // --------------- -template class INSERT> +template class INSERT> void _test(std::size_t iterations, std::size_t container_size, const char *map_name) { std::size_t count = 0; auto t1 = std::chrono::high_resolution_clock::now(); - INSERT insert; + INSERT insert; for (std::size_t i=0; i class INSERT> +template class INSERT> void test(std::size_t iterations, std::size_t container_size) { std::clog << "bench: iterations: " << iterations << " / container_size: " << container_size << "\n"; - _test, V, void, INSERT>(iterations, container_size, " std::map: "); - _test, V, void, INSERT>(iterations, container_size, " std::unordered_map: "); - _test, V, void, INSERT>(iterations, container_size, " phmap::flat_hash_map: "); - _test>, V, int, INSERT> (iterations, container_size, " std::vector: "); + _test, K, V, void, INSERT>(iterations, container_size, " std::map: "); + _test, K, V, void, INSERT>(iterations, container_size, " std::unordered_map: "); + _test, K, V, void, INSERT>(iterations, container_size, " phmap::flat_hash_map: "); + _test>, K, V, int, INSERT> (iterations, container_size, " std::vector: "); std::clog << "\n"; } @@ -153,9 +148,9 @@ int main() // ------------------------------------------------------------------------- std::clog << "\n\n" << "testing with " "\n"; std::clog << "---------------------------------" "\n"; - test(iterations,10); - test(iterations,100); - test(iterations,500); + test(iterations,10); + test(iterations,100); + test(iterations,500); // test with custom_type, which contains two std::string values, and use // a generated string key. This is not very indicative of the speed of the