Topic: [container] diference betwen clear and swap trick


Author: r0d <pons.rodrigue@gmail.com>
Date: Thu, 29 Oct 2009 15:26:55 CST
Raw View
Hello,

i spent a bit time making tests to see wich of this two ways is
fasters (in execution time i mean):

We get an std::vector<T> v. Then we fill it. Then we want to delete it
(erase form memory)

1. v.clear();
2. std::vector<T>().swap( v );

And well, i made some test of quickness to ascertain wich way is the
faster. And what a surprise: it depends! It depends on the version of
the STL, on the platform (linux, windows), on the type of T...

Thus this is my question: is there any know issue about when one way
is better than the other?

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]