Topic: diference betwen clear and swap trick


Author: Kenshin <kenshin.himura.sakabato@gmail.com>
Date: Mon, 30 Nov 2009 12:21:14 CST
Raw View
On Oct 30, 12:26 am, r0d <pons.rodri...@gmail.com> wrote:
> 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-...@netlab.cs.rpi.edu]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html                     ]

Yeah; when clarity of code & intent is paramount :-)
Use v.clear() to clear the elements, the other to shrink elements of
vector.
Since these are well established & understood by virtually all C++
programmers, no other reason beats this, especially in large
projects :-)


--
[ 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                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Thu, 29 Oct 2009 18:03:16 CST
Raw View
On 29 Okt., 22:26, r0d <pons.rodri...@gmail.com> wrote:
> 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?

Calling clear on std::vector does *not* change it's capacity, so
(a) is not the right choice, if you want to get rid of the reserved
memory area. Solution (2) is the only portable way to the give
the implementation the chance to reduce the capacity. This
is not guaranteed, because it is unspecified which capacity
an empty vector has, but v will have the same capacity as
an empty vector, which for most implementations is a capacity
of 0.

The reason for different behaviour is also due to the fact, that
the standard wasn't very clear in C++03 regarding it's general
allocation policy. This became a bit better with

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#329
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#341

For details regarding the intended meaning of clear() see

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#1102

which will soon be closed as NAD because "the standard is correct
as written".

C++0x will go a step further by providing a function shrink_to_fit,
see

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#755

HTH & Greetings from Bremen,

Daniel Kr   gler



--
[ 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                      ]