why is this bzip2 boost code so slow?

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? #include <iostream> #include <sstream> #include <boost/assign/list_of.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/foreach.hpp> #include <boost/format.hpp> #include <boost/lexical_cast.hpp> #include <boost/numeric/conversion/cast.hpp> #include <boost/iostreams/copy.hpp> #include <boost/iostreams/device/back_inserter.hpp> #include <boost/iostreams/filter/bzip2.hpp> #include <boost/iostreams/filter/zlib.hpp> #include <boost/iostreams/filtering_stream.hpp> using namespace std; typedef boost::posix_time::ptime TimeType; TimeType current_time() { return boost::posix_time::microsec_clock::local_time(); } double elapsed_seconds(const TimeType& start_time) { static const double MSEC_PER_SEC = 1000.0; TimeType end_time = current_time(); boost::posix_time::time_duration elapsed = end_time - start_time; return boost::numeric_cast<double>(elapsed.total_milliseconds()) / MSEC_PER_SEC; } string decompress(const string& data) { boost::iostreams::filtering_streambuf<boost::iostreams::input> in; in.push(boost::iostreams::bzip2_decompressor()); in.push(boost::make_iterator_range(data)); string decompressed; boost::iostreams::copy(in, boost::iostreams::back_inserter(decompressed)); return decompressed; } 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; } -- View this message in context: http://www.nabble.com/why-is-this-bzip2-boost-code-so-slow--tp25442062p25442... Sent from the Boost - Users mailing list archive at Nabble.com.

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
participants (2)
-
ChristinaDRS
-
Roland Bock