I have the following test code:
#include <vector>
#include <sstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
class MyObject
{
public:
MyObject()
: m_width(0)
, m_height(0)
, m_data()
{
}
MyObject(int width, int height)
: m_width(width)
, m_height(height)
{
m_data.resize(m_width * m_height);
for (std::vector<bool>::iterator iter = m_data.begin(); iter != m_data.end(); ++iter)
*iter = 1; // This could be 1 or 0 but I'm setting it to 1 for simplicity.
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& archive, const unsigned int version)
{
archive & m_width;
archive & m_height;
archive & m_data;
}
private:
int m_width;
int m_height;
std::vector<bool> m_data;
};
int main()
{
int width = 1000;
int height = 1000;
MyObject original(width, height);
std::stringstream outputStream(std::ios_base::binary | std::ios_base::out | std::ios_base::in);
boost::archive::binary_oarchive outputArchive(outputStream, boost::archive::no_header);
outputArchive << original;
const std::string& originalData = outputStream.str();
int size = originalData.size();
char *data = new char[size];
memcpy(&data[0], originalData.c_str(), size);
std::string buffer;
buffer.insert(0, &data[0], size);
std::stringstream inputStream(buffer, std::ios_base::binary | std::ios_base::out | std::ios_base::in);
boost::archive::binary_iarchive inputArchive(inputStream, boost::archive::no_header);
clock_t begin = clock();
MyObject restore;
inputArchive >> restore;
clock_t end = clock();
std::cout << "Elapsed seconds: " << double(end - begin) / CLOCKS_PER_SEC;
delete [] data;
return 0;
}
On my PC:
- when "width" and "height" = 100 ==> Elapsed seconds = 0.238
- when "width" and "height" = 1000 ==> Elapsed seconds = 23.616
What can I do to speed things up for case 2?