Created
July 25, 2019 05:55
-
-
Save martinus/1502163058b247b6cee318ae4b605f89 to your computer and use it in GitHub Desktop.
simple benchmark of tsl, robin_hood, and std::unordered_map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <chrono> | |
#include <unordered_map> | |
#include "tsl/robin_map.h" | |
#include "robin_hood.h" | |
using namespace std; | |
using my_clock = std::chrono::high_resolution_clock; | |
int main() | |
{ | |
int32_t iterate_num = 1000000; | |
int trials = 100; | |
robin_hood::unordered_map<string, string> robin_map; | |
double robin_insert = 0.0; | |
double robin_iter = 0.0; | |
double robin_find = 0.0; | |
size_t robin_result = 0; | |
tsl::robin_map<string, string> tsl_map; | |
double tsl_insert = 0.0; | |
double tsl_iter = 0.0; | |
double tsl_find = 0.0; | |
size_t tsl_result = 0; | |
unordered_map<string, string> def_map; | |
double def_insert = 0.0; | |
double def_iter = 0.0; | |
double def_find = 0.0; | |
size_t def_result = 0; | |
// Insert | |
auto start = my_clock::now(); | |
{ | |
for (int i=0; i<10; ++i) { | |
robin_map.clear(); | |
for (int32_t i = 0; i < 1000000; i++) | |
{ | |
robin_map.insert({ to_string(i), "hello world naxren epta" }); | |
} | |
} | |
} | |
robin_result += robin_map.size(); | |
robin_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 : robin_map) | |
{ | |
robin_result += keyVal.second.size(); | |
} | |
} | |
} | |
robin_iter = std::chrono::duration_cast<std::chrono::duration<double> >(my_clock::now() - start).count(); | |
// Find | |
start = my_clock::now(); | |
{ | |
for (int32_t i = 0; i < 5000000; i++) { | |
auto it = robin_map.find(std::to_string(i)); | |
if (it != robin_map.end()) | |
{ | |
robin_result += it->second.size(); | |
} | |
} | |
} | |
robin_find = std::chrono::duration_cast<std::chrono::duration<double> >(my_clock::now() - start).count(); | |
// Insert | |
start = my_clock::now(); | |
{ | |
for (int i=0; i<10; ++i) { | |
tsl_map.clear(); | |
for (int32_t i = 0; i < 1000000; i++) | |
{ | |
tsl_map.insert({ to_string(i), "hello world naxren epta" }); | |
} | |
} | |
} | |
tsl_result += tsl_map.size(); | |
tsl_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 : tsl_map) | |
{ | |
tsl_result += keyVal.second.size(); | |
} | |
} | |
} | |
tsl_iter = std::chrono::duration_cast<std::chrono::duration<double> >(my_clock::now() - start).count(); | |
// Find | |
start = my_clock::now(); | |
{ | |
for (int32_t i = 0; i < 5000000; i++) { | |
auto it = tsl_map.find(std::to_string(i)); | |
if (it != tsl_map.end()) | |
{ | |
tsl_result += it->second.size(); | |
} | |
} | |
} | |
tsl_find = std::chrono::duration_cast<std::chrono::duration<double> >(my_clock::now() - start).count(); | |
// Insert | |
start = my_clock::now(); | |
{ | |
for (int i=0; i<10; ++i) { | |
def_map.clear(); | |
for (int32_t i = 0; i < 1000000; i++) | |
{ | |
def_map.insert({ to_string(i), "hello world naxren epta" }); | |
} | |
} | |
} | |
def_result += robin_map.size(); | |
def_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 : def_map) | |
{ | |
def_result += keyVal.second.size(); | |
} | |
} | |
} | |
def_iter = std::chrono::duration_cast<std::chrono::duration<double> >(my_clock::now() - start).count(); | |
// Find | |
start = my_clock::now(); | |
{ | |
for (int32_t i = 0; i < 5000000; i++) { | |
auto it = def_map.find(std::to_string(i)); | |
if (it != def_map.end()) | |
{ | |
def_result += it->second.size(); | |
} | |
} | |
} | |
def_find = std::chrono::duration_cast<std::chrono::duration<double> >(my_clock::now() - start).count(); | |
// Result | |
cout << "----- [Benchmark] -----\n" << "[R] Insert Time: " << robin_insert << "\n[T] Insert Time: " << tsl_insert << "\n[D] Insert Time: " << def_insert << "\n" << endl; | |
cout << "[R] Iter Time: " << robin_iter << "\n[T] Iter Time: " << tsl_iter << "\n[D] Iter Time: " << def_iter << "\n" << endl; | |
cout << "[R] Find Time: " << robin_find << "\n[T] Find Time: " << tsl_find << "\n[D] Find Time: " << def_find << "\n" << endl; | |
std::cout << "[R] result: " << robin_result << std::endl; | |
std::cout << "[T] result: " << tsl_result << std::endl; | |
std::cout << "[D] result: " << def_result << std::endl; | |
} |
Author
martinus
commented
Jul 25, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment