Which is more efficient / neat: flushing an existing string stream or creating a new one?

A simple question only out of curiosity.

Several methods in the class must use a string stream or, in particular, ostringstream.

1) Have the stringstream variable as a member of the class, and then just clear it before using it, i.e.msg.str("")

2) Create a new stringstream variable locally in each method every time you need to use it.

What is the best way to implement this, in terms of efficiency and accuracy?

My hunch is option 1, but not sure if the initial construct combined with each call to str () will be worse?

PS I read Initialization .. which one is more efficient? and which is faster / more efficient? , my next step would be to consider profiling and writing a small test application, but I could ask, maybe faster :-)

+5
source share
1 answer

stringstream . istringstream, ostringstream, - . ; - , , - . msg.str("") . reset , , , , xalloc.

+7

All Articles