Boost logo

Boost Users :

Subject: [Boost-users] why is this bzip2 boost code so slow?
From: ChristinaDRS (mail_at_[hidden])
Date: 2009-09-14 15:48:47


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--tp25442062p25442062.html
Sent from the Boost - Users mailing list archive at Nabble.com.

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