Skip to content

Instantly share code, notes, and snippets.

@martinus
Created July 27, 2019 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinus/5a9f0548ab8ab745bd2683dcd9781c18 to your computer and use it in GitHub Desktop.
Save martinus/5a9f0548ab8ab745bd2683dcd9781c18 to your computer and use it in GitHub Desktop.
simple benchmark of robin_hood map, updated
#include <iostream>
#include <string>
#include <chrono>
#include <unordered_map>
#include "tsl/robin_map.h"
#include "robin_hood.h"
using my_clock = std::chrono::high_resolution_clock;
template<typename Map>
void bench(char const* title) {
int32_t iterate_num = 1000000;
int trials = 100;
size_t result = 0;
Map map;
// Insert
auto start = my_clock::now();
{
for (int i = 0; i < 10; ++i) {
map.clear();
for (int32_t i = 0; i < 1000000; i++)
{
map.emplace(std::to_string(i), "hello world naxren epta");
}
}
}
result += map.size();
double t_insert = std::chrono::duration_cast<std::chrono::duration<double>>(my_clock::now() - start).count();
// Iter
start = my_clock::now();
{
for (size_t i = 0; i < trials; ++i) {
for (auto const& keyVal : map)
{
result += keyVal.second.size();
}
}
}
double t_iter = std::chrono::duration_cast<std::chrono::duration<double>>(my_clock::now() - start).count();
// Find
start = my_clock::now();
{
for (size_t x = 0; x < 10; ++x) {
for (int32_t i = 0; i < 5000000; i++) {
auto it = map.find(std::to_string(i));
if (it != map.end())
{
result += it->second.size();
}
}
}
}
double t_find = std::chrono::duration_cast<std::chrono::duration<double>>(my_clock::now() - start).count();
printf("%6.2f insert %6.2f iter %6.2f find. %lld result: %s\n", t_insert, t_iter, t_find, result, title);
}
int main()
{
bench<robin_hood::unordered_map<std::string, std::string>>("robin_hood::unordered_map<std::string, std::string>");
bench<robin_hood::unordered_node_map<std::string, std::string>>("robin_hood::unordered_node_map<std::string, std::string>");
bench<robin_hood::unordered_flat_map<std::string, std::string>>("robin_hood::unordered_flat_map<std::string, std::string>");
bench<tsl::robin_map<std::string, std::string>>("tsl::robin_map<std::string, std::string>");
bench<std::unordered_map<std::string, std::string>>("std::unordered_map<std::string, std::string>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment