I'm using std::multimap
with tens of millions of elements. The key is a pointer, and the data is a shared pointer to a class. Sometimes I need to remove a few elements, sometimes many elements, and sometimes all elements. I'm currently using std::erase_if
and I'm wondering about performance for the case where all elements are removed.
The first part of the question is: If all elements are being removed, would clear
be substantially faster than erase_if
?
I'm guessing the answer is "yes," which leads to the next question. A problem is that, at the point of the erase_if
call, I don't know whether or not all elements will be removed. So, I could do something like count_if
first, and if the count equals the map size then do clear
, and otherwise do the erase_if
. When removing a few elements, I'm guessing the use of count_if
followed by erase_if
would roughly double the time, which I think would be ok. But when removing all elements, how much would the count_if
reduce the gain of using clear
?
(I'm using gcc 12.3.0.)
std::(unordered_)map
whereT
is another container.