I need to make many unions of an ordered set of integers (I would like to avoid duplicates, but this is normal, if any).
This is the code with the best performance:
// 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 callgrind tool) tells me that I spend 99% of the time doing sorting.
This is what I have tried so far:
- Using std :: set (very poor performance)
- Using std :: set_union (poor performance)
- saving heap with std :: push_heap (50% slower)
Is there any hope of getting some kind of performance? I can change my containers and use boost and maybe some other libs (depending on its license).
EDIT integers can be like 10,000,000 EDIT 2 gave an example of how I use it, due to some confusion
source share