Without performing a performance test, I would tell you that it is intuitively clear that Options 2 (reuse) is slightly faster because you do not create or destroy the container each time. When you destroy a container, it necessarily clears it, otherwise there will be a memory leak.
However, performing a performance test gives different results:
- For std :: list, it seems that
clear takes longer than the destructor. - For std :: set, the results were almost identical.
- For std :: map, the results were close, but option 1 (destructor) was a bit faster.
- For std :: vector, the results were like a list (although a vector vector is much faster)
So overall, it seems that parameter1 is a little faster. I worked with VS2012, YMMV
David Hope
source share