What does your profiler say?
It depends largely on the object, the compiler and the types of use you
are making of it. The one time I benchmarked it (and std::string
in
the g++
library, being reinitialized each time through a loop),
reconstructing the object each time in the loop was faster. On the
other hand, most of the other standard containers retain their memory even
when emptied; in such cases, if you define the container outside of the
loop, it will (usually) arrive at its final size fairly quickly, after
which there will be no other allocations.
And of course, you have to consider just how difficult it is to restore
the object to a pristine state. It's almost impossible for objects
derived from std::ios_base
, for example; you almost always want to use
a new std::ostringstream
, rather than trying to reuse an existing one.
Despite the cost.