How come that random deletion from a std::vector is faster than a std::list? What I’m doing to speed it up is swapping the random element with the last and then deleting the last.
I would have thought that the list would be faster since random deletion is what it was built for.
for(int i = 500; i < 600; i++){
swap(vector1[i], vector1[vector1.size()-1]);
vector1.pop_back();
}
for(int i = 0; i < 100; i++){
list1.pop_front();
}
Results (in seconds):
Vec swap delete: 0.00000909461232367903
List normal delete: 0.00011785102105932310
What you’re doing is not random deletion though. You’re deleting from the end, which is what vectors are built for (among other things).
And when swapping, you’re doing a single random indexing operation, which is also what vectors are good at.