Boost logo

Boost Users :

Subject: Re: [Boost-users] Fastest way of serializing a huge vector of ints into a human readable string
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2009-02-27 13:06:54


Roland that's not the fastest way with some other reagards, because
stringstream as all iostream classes are designed to deal with thread safe
apps and call lock/unlock pairs for _every_ character inserted. This
operation is done by constructing the ostream::sentry guard. To be more
efficient write your output to stream_buf classes.

try adapting this example to your needs:
http://codepad.org/ujfeLB72

When I did some tests, this brought me performance factor of 3 or 4 over
common iostream.

With Kind Regards,
Ovanes

On Fri, Feb 27, 2009 at 6:46 PM, Roland Bock <rbock_at_[hidden]> wrote:

> Hi,
>
> I have a program which produces a vector of integers (several million
> entries). I need to write that into a human-readable string of space
> separated numbers.
>
> I wonder, what would be the fastest way?
>
> My first attempt was
>
> stringstream resultStream;
> copy(integers.begin(), integers.end(), ostream_iterator<int>(resultStream,
> " "));
> string result = resultStream.str();
>
>
> But that requires copying the string.
>
>
> Thus I was wondering if I could use boost to write to the target string
> directly?
>
> boost::iostreams allow me to do the following:
>
>
> string result;
> boost::iostreams::filtering_ostream
> out(boost::iostreams::back_inserter(result));
> copy(integers.begin(), integers.end(), ostream_iterator<uint64>(out, " "));
>
> This is faster by almost a factor of 2.
>
> Any ideas how to increase speed even more?
>
>
> Thanks in advance,
>
> Roland
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net