I want to meet some performance requirements in my C++ project. I have found that using push_back
to an std::vector
has a performance penalty. Also when I use find
for the elements of an std::vector
it also takes more time than using an unordered_map
.
Is it OK if I use emplace
on an unordered_map
? Can the save/get cycle be 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) is
#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 to meet some performance requirements in my C++ project.
Here are the steps you should do:
You would not get a good answer just to look at a container in vanilla example. You need to optimize your program, not example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.