boost - Custom hash in std::unordered map in C++ -
i want meet performance requirements in c++ project. have found using push_back
std::vector
has performance penalty. when use find
elements of std::vector
takes more time using unordered_map
.
is ok if use emplace
on unordered_map
? can save/get cycle done faster?
g++ driver.cpp -std=c++11
the header file is
#ifndef custom_unor_map_hpp #define custom_unor_map_hpp #include <boost/algorithm/string/predicate.hpp> #include <boost/functional/hash.hpp> #include <unordered_map> namespace pe { struct pe_hash { size_t operator()(const std::string& key) const { std::size_t seed = 0; std::locale locale; for(auto c : key) { boost::hash_combine(seed, std::toupper(c, locale)); } return seed; } }; struct pe_key_eq { bool operator()(const std::string& l, const std::string& r) const { return boost::iequals(l, r); } }; using pe_map = std::unordered_map<std::string, std::string, pe_hash, pe_key_eq>; } #endif
the driver file (main.cpp)
#include "pemap.hpp" #include <iostream> template <typename t> inline const std::string& get_map_value(const t& container, const std::string& key) { if (container.count(key)) { return container.find(key)->second; } static std::string empty; return empty; } int main(int argc, char** argv) { std::string key = "key"; std::string val = "value1"; std::string val2 = "value2"; pe::pe_map container; container.emplace(std::move(key), std::move(val)); container.emplace(std::move(key), std::move(val2)); std::cout << get_map_value( container, "key") << std::endl; }
i want meet performance requirements in c++ project.
here steps should do:
- run profiler
- identify code , data manipulation takes time
- think better algorithm and/or data organization case (container type 1 of them)
- if performance enough, done, if not try optimize code, takes time
you not answer @ container in vanilla example. need optimize program, not example.
Comments
Post a Comment