Boost logo

Boost Users :

Subject: [Boost-users] Boost::serialization problem when use archive in a loop
From: mikesam (ingens_at_[hidden])
Date: 2011-04-20 10:32:06


I have a problem with serialization, and I do not know how to solve it. Time
to serialize the class a lot of growing when I do it in a loop. The first
iteration takes about 40 ms, for example, but any other takes about 400 ms.

class NODE
{
        public:
                
                NODE(){}

                NODE* add(NODE* node)
                {
                        children.push_back(node);
                        return node;
                }

                ~NODE()
                {
                        for(vector<NODE*>::const_iterator it = children.begin(); it !=
children.end(); ++it)
                        {
                                delete *it;
                        }
                }

                vector<NODE*> children;

        private:
                friend class boost::serialization::access;

                template <typename Archive>
                void serialize(Archive &ar, const unsigned int version)
                {
                        ar & children;
                }
};

void save(NODE* main)
{
        std::ofstream stream("test", std::ios::binary);
        boost::archive::binary_oarchive oa(stream, boost::archive::no_header);
        oa << main;
}

int main()
{
        boost::timer t;

        NODE* main(new NODE());

        for (int i=0; i<2000;i++)
        {
                NODE* subnode = main->add( new NODE());
                for (int k=0; k<20; k++) subnode->add( new NODE());
        }
                for (int i=0; i<4; i++)
                {
                        t.restart();
                        save(main);
                        cout<<"serialize: "<<t.elapsed()<<endl;
                }
        delete main;
        return 0;
}

--
View this message in context: http://boost.2283326.n4.nabble.com/Boost-serialization-problem-when-use-archive-in-a-loop-tp3463193p3463193.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