Reputation: 3553
I am trying to store some information of a graph in an unordered_map. Each edge has some parameters. There are 120 edges, and each edge has 90*2 different parameters.
I have the following implementation of std::unordered_map<>
typedef std::tuple<int, int, int, int> metric_tuple_key; // metric tuple key
// define a hash function for this metric_tuple_key tuple
struct m_KeyHash : public std::unary_function<metric_tuple_key, std::size_t> {
std::size_t operator()(const metric_tuple_key& k) const {
// the magic operation below makes collisions less likely than just the standard XOR
std::size_t seed = std::hash<int>()(std::get<0>(k));
seed ^= std::hash<int>()(std::get<1>(k)) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= std::hash<int>()(std::get<2>(k)) + 0x9e3779b97f4a7c15 + (seed << 6) + (seed >> 2);
return seed ^ (std::hash<char>()(std::get<3>(k)) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
};
// define the comparison operator for this metric_tuple_key tuple
struct m_KeyEqual : public std::binary_function<metric_tuple_key, metric_tuple_key, bool> {
bool operator()(const metric_tuple_key& v0, const metric_tuple_key& v1) const {
return (std::get<0>(v0) == std::get<0>(v1) && std::get<1>(v0) == std::get<1>(v1) &&
std::get<2>(v0) == std::get<2>(v1) && std::get<3>(v0) == std::get<3>(v1));
}
};
std::unordered_map<metric_tuple_key, double, m_KeyHash, m_KeyEqual> _metrics;
I was able to insert the values into _metrics
by creating a tuple key.
Now, I want to get some values from _metrics
when a key is specified.
//Retrieve around 120 double values. Total number of entries in _metrics is 21600
double k = _metrics.at((std::make_tuple(m, k, edge.first, edge.second))). //do this 120 times
This turns out to be very slow (almost 400 ms). I was hoping that it would take around a millisecond or less.
Am I doing anything wrong or is std::unordered_map not suitable for my use case. I had used python dictionaries before to solve this same issue and retrieval of values is near instantaneous in python dicts
edit: some unordered_map stats:
max_load_factor: 1
size: 21600
bucket_count: 25717
load_factor: 0.839911
Edit: Timer Code:
#include <chrono>
#include <iostream>
#include <iomanip>
class Timer {
private:
std::chrono::time_point<std::chrono::steady_clock> start , stop;;
public:
void startClock();
void stopClock();
void elapsedTime(std::string &message);
};
#include "Timer.hpp"
void Timer::startClock() {
start = std::chrono::steady_clock::now();
}
void Timer::stopClock() {
stop = std::chrono::steady_clock::now();
}
void Timer::elapsedTime(std::string &message) {
auto diff = stop - start;
std::cout << "Elapsed time for " <<message<< " " << std::setprecision(13) <<std::chrono::duration <double, std::milli> (diff).count() << " ms" << std::endl;
}
and the Time measurement is
T_met.startClock();
for (const auto& edges: list_of_arcs())
{
double k = _metrics.at((std::make_tuple(m, k, edge.first, edge.second)))
}
T_met.stopClock();
Upvotes: 3
Views: 590
Reputation: 1072
Search duration is depended of quality of your hash. For test, you can use "map" - it has stable seach duration. If map is faster than unordered map - your hash is bad.
Upvotes: 2