I need to make lots of unions of ordered set of integers (I would like to avoid duplicates, but it is okay if there are).
This is the code with the best performance so far :
// some code added for better understanding
std::vector< std::pair<std::string, std::vector<unsigned int> > vec_map;
vec_map.push_back(std::make_pair("hi", std::vector<unsigned int>({1, 12, 1450});
vec_map.push_back(std::make_pair("stackoverflow", std::vector<unsigned int>({42, 1200, 14500});
std::vector<unsigned int> match(const std::string & token){
auto lower = std::lower_bound(vec_map.begin(), vec_map.end(), token, comp2());
auto upper = std::upper_bound(vec_map.begin(), vec_map.end(), token, comp());
std::vector<unsigned int> result;
for(; lower != upper; ++lower){
std::vector<unsigned int> other = lower->second;
result.insert(result.end(), other.begin(), other.end());
}
std::sort(result.begin(), result.end()); // This function eats 99% of my running time
return result;
}
valgrind (using the tool callgrind) tells me that I spend 99% of the time doing the sort.
This is what I tried so far :
- Using std::set (very bad performance)
- Using std::set_union (bad performance)
- maintaining a heap with std::push_heap (50% slower)
Is there any hope to gain somehow some performance? I can change my containers and use boost, and maybe some other lib (depending on its licence).
EDIT integers can be as big a 10 000 000 EDIT 2 gave some example of how I use it, because of some confusion