Siemel Naran
2003-09-09 17:56:35 UTC
unsigned int size = 300000; // this value actually comes from
another func
std::string s;
s.resize(size);
std::ostringstream os(s);
The line s.resize(size) gives 's' a size of 3 million elements. The lastanother func
std::string s;
s.resize(size);
std::ostringstream os(s);
line makes a copy of 's', and if you string is not reference counted, then
it copies all 3 million chars in the string. Could be bad performance.
If you say s.reserve(size) then 's' has 0 chars but space to hold 3 million.
The last line still makes a copy of 's', but per table 43 of the standard,
the capacity() in the copied string may still be zero, not 3 million. So
you still have bad performance.
I think that an enhancement to std::basic_stringbuf and the corresponding
iostream classes might be the answer.
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]