Replies: 1 comment 2 replies
-
Interesting. You happen to use a very special case in your test, where the key space maps exactly to the map (indeed you could use a The regular
I have slightly modified your example, where instead of inserting keys 0 through N, I insert random integer keys, which is probably a more realistic example. When doing this, phmap is faster.
#include <iostream>
#include <unordered_map>
#include <parallel_hashmap/phmap.h>
#include <random>
using namespace std;
static int N = 100000000;
int main() {
std::random_device rd; // a seed source for the random number engine
std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<size_t> u (0, N*27);
{
std::unordered_map<int, int> map;
map.reserve(N);
auto t = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++) {
map[u(gen)] = i;
}
auto duration = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - t).count();
std::cout << "STL: Done in " << duration << "s." << std::endl;
}
{
phmap::flat_hash_map<int, int> map;
map.reserve(N);
auto t = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++) {
map[u(gen)] = i;
}
auto duration = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - t).count();
std::cout << "flat_hash_map: Done in " << duration << "s." << std::endl;
}
{
phmap::parallel_flat_hash_map<
int,
int,
phmap::priv::hash_default_hash<int>,
phmap::priv::hash_default_eq<int>,
phmap::priv::Allocator<phmap::priv::Pair<const int, int>>,
6,
std::mutex> map;
map.reserve(N);
auto t = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++) {
map[u(gen)] = i;
}
auto duration = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - t).count();
std::cout << "parallel_flat_hash_map: Done in " << duration << "s." << std::endl;
}
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
Hi. I have been a user of this repo since 2022. Today for a different project, I finally sat down and ran some quick benchmarks on 100M insertions. But to my surprise, the STL
unordered_map
is a lot faster than theflat_hash_map
orparallel_flat_hash_map
. I must be doing something wrong here.Added to CMakeLists.txt:
I tested using the 1.3.8 version, but assuming newer versions show similar behavior.
Beta Was this translation helpful? Give feedback.
All reactions