adding the robin hood code
This commit is contained in:
committed by
JamesFlare1212
parent
b7bf00f731
commit
dfaacfabff
40
lectures/optimization/robin_hood/README.md
Normal file
40
lectures/optimization/robin_hood/README.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# `robin_hood::unordered_map` — Fast Hash Map for C++
|
||||
|
||||
`robin_hood::unordered_map` is a high-performance hash table implementation designed as a faster alternative to `std::unordered_map`, with better cache locality and reduced memory overhead.
|
||||
|
||||
---
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Faster lookups** and insertions compared to `std::unordered_map`
|
||||
- **Robin Hood hashing**: minimizes probe length differences to keep performance stable
|
||||
- **Header-only**: easy to integrate
|
||||
- **Memory efficient**: lower memory usage due to compact internal representation
|
||||
- **Supports custom hash and equality functions**
|
||||
|
||||
---
|
||||
|
||||
## Installation
|
||||
|
||||
Include the header file in your project:
|
||||
|
||||
```cpp
|
||||
#include "robin_hood.h"
|
||||
```
|
||||
|
||||
You can get the header from:
|
||||
|
||||
GitHub: https://github.com/martinus/robin-hood-hashing
|
||||
|
||||
## Benchmarks
|
||||
|
||||
Compile and run this [program](robin_hood_test.cpp) to see the performance difference; you will need to both [robin_hood_test.cpp](robin_hood_test.cpp) and [robin_hood.h](robin_hood.h).
|
||||
|
||||
The program performs 2 millions times of insert and lookup on an std::unordered_map container; and then performs 2 million times of insert and lookup on a robin_hood::unordered_map container. As can be seen, the robin hood test runs much faster.
|
||||
|
||||
```console
|
||||
$g++ -o robin_hood_test robin_hood_test.cpp
|
||||
$./robin_hood_test
|
||||
[std::unordered_map] Time: 1145 ms. Sum: -1455759936
|
||||
[robin_hood::unordered_map] Time: 918 ms. Sum: -1455759936
|
||||
```
|
||||
2544
lectures/optimization/robin_hood/robin_hood.h
Normal file
2544
lectures/optimization/robin_hood/robin_hood.h
Normal file
File diff suppressed because it is too large
Load Diff
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