Re: Preallocate ostringstream buffer




"Jason Winnebeck" <gillius-ng2@xxxxxxxxxxxxxxxxxx> skrev i meddelandet
news:esu8rpiXGHA.1228@xxxxxxxxxxxxxxxxxxxxxxx
P.J. Plauger wrote:
And it's generally unnecessary with the library shipped with VC++
these days, since the buffer grows exponentially. Execution time
is not a problem. If space limitations are a problem, it's still
particularly easy to use strstream with a preallocated buffer.

I've always wondered about this problem with exponential growth. A
common technique here is to always increase by a power-of-2, so the
argument that the time spent reallocating in insignificant compared
to the task itself, but this can become an issue with space. I
suppose you mention that in your last sentence, but is there a
better way in the general case? Probably not. I suppose worst case
scenario is that your program takes twice the memory than it needs.

A common compromise here is to grow by a factor of 1.5 instead of 2.
This halves the theoretical worst case, without affecting runtime much
at all. It also reduces memory fragmentation a bit.


Bo Persson


.



Relevant Pages

  • Re: Preallocate ostringstream buffer
    ... Execution time ... If space limitations are a problem, ... particularly easy to use strstream with a preallocated buffer. ... It also reduces memory fragmentation a bit. ...
    (microsoft.public.vc.stl)
  • Re: Preallocate ostringstream buffer
    ... Execution time ... If space limitations are a problem, ... particularly easy to use strstream with a preallocated buffer. ... I had an issue with a similar algorithm in an embedded system that I was part of when the buffer went from 512k to 1M to store what was 99% of the time a max of about 512k bytes of data, and the 512k waste over a few buffers times was enough to kill off the system. ...
    (microsoft.public.vc.stl)
  • Re: process a BIG string
    ... I'm not sure that increasing the execution time will be the solution, ... i need to process every character in a file, so i open the file read ... in buffers of about 8192bytes and process each buffer, ... the problem is that with large filesi get a script error(Fatal ...
    (comp.lang.php)

Quantcast