adding the robin hood code
This commit is contained in:
committed by
JamesFlare1212
parent
b7bf00f731
commit
dfaacfabff
58
lectures/optimization/robin_hood/robin_hood_test.cpp
Normal file
58
lectures/optimization/robin_hood/robin_hood_test.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include "robin_hood.h"
|
||||
|
||||
constexpr size_t NUM_ELEMENTS = 2'000'000;
|
||||
|
||||
void benchmark_std() {
|
||||
std::unordered_map<std::string, int> map;
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// Insert
|
||||
for (size_t i = 0; i < NUM_ELEMENTS; ++i) {
|
||||
map["key" + std::to_string(i)] = i;
|
||||
}
|
||||
|
||||
// Lookup
|
||||
int sum = 0;
|
||||
for (size_t i = 0; i < NUM_ELEMENTS; ++i) {
|
||||
sum += map["key" + std::to_string(i)];
|
||||
}
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::cout << "[std::unordered_map] Time: "
|
||||
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
|
||||
<< " ms. Sum: " << sum << "\n";
|
||||
}
|
||||
|
||||
void benchmark_robin() {
|
||||
robin_hood::unordered_map<std::string, int> map;
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
// Insert
|
||||
for (size_t i = 0; i < NUM_ELEMENTS; ++i) {
|
||||
map["key" + std::to_string(i)] = i;
|
||||
}
|
||||
|
||||
// Lookup
|
||||
int sum = 0;
|
||||
for (size_t i = 0; i < NUM_ELEMENTS; ++i) {
|
||||
sum += map["key" + std::to_string(i)];
|
||||
}
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::cout << "[robin_hood::unordered_map] Time: "
|
||||
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
|
||||
<< " ms. Sum: " << sum << "\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
benchmark_std();
|
||||
benchmark_robin();
|
||||
benchmark_robin();
|
||||
benchmark_std();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user