using vector::erase for the whole range
Is
v.erase(v.begin(), v.end());
Just as fast as
v.clear();
?
I don't care about little overheads such as extra function calls etc, the
compiler will inline that stuff.
The reason I ask is because I have code like the following:
v.erase(v.begin(), last_it);
last_it will usually be the end iterator, but not always. I know that
erasing a vector not from the end has a cost, because later elements in
the vector will need to be copied down. In the case where last_it is not
the end iterator (rare), I can live with that. But I don't want to
introduce such overhead when I basically want to clear the vector. So I
considered writing my code like this:
if (last_it == v.end())
{
v.clear();
}
else
{
v.erase(v.begin(), last_it);
}
I would like to know whether this is necessary to avoid a performance
penalty for erasing the whole vector. I would prefer to keep my code clear
and use a single-line statement if there is no penalty.
No comments:
Post a Comment