Boost logo

Boost Users :

Subject: Re: [Boost-users] why is this bzip2 boost code so slow?
From: Roland Bock (rbock_at_[hidden])
Date: 2009-09-14 16:41:28


ChristinaDRS wrote:
> Hello,
>
> This code with bzip2 takes 185 seconds to do 10000 iterations of compressing
> and uncompressing. This seems way too high. When I replace the bzip2 code
> with zlib code it take roughly 7 seconds. Anyone know what might be wrong?
>
[...]
> int main()
> {
> TimeType start_time = current_time();
> string compressed, received_data;
> boost::iostreams::filtering_streambuf<boost::iostreams::output> out, in;
> const string send_data = "boo! how are you?";
>
> for(int i = 0; i < 10000; ++i)
> {
> //compress
> out.push(boost::iostreams::bzip2_compressor());
> out.push(boost::iostreams::back_inserter(compressed));
> boost::iostreams::copy(boost::make_iterator_range(send_data), out);
> const string compressed1=compressed;
> //decompress
> received_data = decompress(compressed);
> }
> cout << "time elapsed: " << elapsed_seconds(start_time) << endl;
> }

Isn't this line a bit of a problem?

> out.push(boost::iostreams::back_inserter(compressed));

Basically, with each iteration you add more data to the string
"compressed". I can't tell you why zip is so much faster, but I guess it
somehow ignores the additional bytes where bzip doesn't.

I also think the previous line should not be inside the for-loop. Not
sure about the effect, though. I am a bit surprised that it doesn't die
with an exception.

With the main function below, I have 10000 iterations in less than a
second on my old machine :-)

<snip>
int main()
{
     TimeType start_time = current_time();
     string compressed, received_data;
     boost::iostreams::filtering_streambuf<boost::iostreams::output>
out, in;
     const string send_data = "boo! how are you?";
     out.push(boost::iostreams::bzip2_compressor());

     for(int i = 0; i < 10000; ++i)
     {
         compressed.clear();
         //compress
         out.push(boost::iostreams::back_inserter(compressed));
         boost::iostreams::copy(boost::make_iterator_range(send_data), out);
         //decompress
         received_data = decompress(compressed);
     }
     cout << "time elapsed: " << elapsed_seconds(start_time) << endl;
}
</snip>

Regards,

Roland


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