1. <legend id='jRzQE'><style id='jRzQE'><dir id='jRzQE'><q id='jRzQE'></q></dir></style></legend>

  2. <tfoot id='jRzQE'></tfoot>
    <i id='jRzQE'><tr id='jRzQE'><dt id='jRzQE'><q id='jRzQE'><span id='jRzQE'><b id='jRzQE'><form id='jRzQE'><ins id='jRzQE'></ins><ul id='jRzQE'></ul><sub id='jRzQE'></sub></form><legend id='jRzQE'></legend><bdo id='jRzQE'><pre id='jRzQE'><center id='jRzQE'></center></pre></bdo></b><th id='jRzQE'></th></span></q></dt></tr></i><div id='jRzQE'><tfoot id='jRzQE'></tfoot><dl id='jRzQE'><fieldset id='jRzQE'></fieldset></dl></div>
    1. <small id='jRzQE'></small><noframes id='jRzQE'>

        <bdo id='jRzQE'></bdo><ul id='jRzQE'></ul>

      提升 C++ 序列化开销

      Boost C++ Serialization overhead(提升 C++ 序列化开销)
      1. <legend id='8wNnv'><style id='8wNnv'><dir id='8wNnv'><q id='8wNnv'></q></dir></style></legend>
          <tbody id='8wNnv'></tbody>

      2. <i id='8wNnv'><tr id='8wNnv'><dt id='8wNnv'><q id='8wNnv'><span id='8wNnv'><b id='8wNnv'><form id='8wNnv'><ins id='8wNnv'></ins><ul id='8wNnv'></ul><sub id='8wNnv'></sub></form><legend id='8wNnv'></legend><bdo id='8wNnv'><pre id='8wNnv'><center id='8wNnv'></center></pre></bdo></b><th id='8wNnv'></th></span></q></dt></tr></i><div id='8wNnv'><tfoot id='8wNnv'></tfoot><dl id='8wNnv'><fieldset id='8wNnv'></fieldset></dl></div>
              <tfoot id='8wNnv'></tfoot>

                <bdo id='8wNnv'></bdo><ul id='8wNnv'></ul>

                <small id='8wNnv'></small><noframes id='8wNnv'>

                本文介绍了提升 C++ 序列化开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用以下代码测量序列化开销

                I'm trying to measure serialization overhead with following code

                    const int message_size=1000;
                
                    std::vector<short> message(message_size);
                
                    std::string s((char*)(&message[0]), message_size * sizeof(short));
                
                    double size= 1000*sizeof(short);
                    double size2= s.size();
                    double overhead = size2 - size; //is zero
                

                正确吗?(这是从矢量序列化)

                如何衡量序列化开销?- 主要问题是测量序列化向量.我可以使用 Boost 进行序列化.

                How can I measure serialization overhead? - main problem is to measure serialized vector. I can use Boost for serialization.

                推荐答案

                这个通用的测试平台应该能让你做出决定:看它生活在 Coliru 上

                This generic test bed should enable you to decide: see it Live On Coliru

                #include <boost/archive/binary_oarchive.hpp>
                #include <boost/archive/text_oarchive.hpp>
                #include <boost/archive/xml_oarchive.hpp>
                #include <boost/fusion/adapted/boost_tuple.hpp>
                #include <boost/make_shared.hpp>
                #include <boost/phoenix.hpp>
                #include <boost/serialization/array.hpp>
                #include <boost/serialization/shared_ptr.hpp>
                #include <boost/serialization/string.hpp>
                #include <boost/serialization/vector.hpp>
                #include <boost/tuple/tuple.hpp>
                #include <iostream>
                #include <sstream>
                
                namespace detail
                {
                    struct add_to_archive_f 
                    {
                        template <typename, typename> struct result { typedef void type; };
                        template <typename Archive, typename T> 
                            void operator()(Archive& ar, T const& t) const {
                                ar << BOOST_SERIALIZATION_NVP(t);
                            }
                    };
                
                    static const boost::phoenix::function<add_to_archive_f> add_to_archive { };
                }
                
                template <typename Archive = boost::archive::binary_oarchive, typename... Data>
                size_t archive_size(Data const&... data)
                {
                    std::ostringstream oss;
                    Archive oa(oss);
                
                    boost::fusion::for_each(boost::make_tuple(data...), 
                            detail::add_to_archive(
                                boost::phoenix::ref(oa), 
                                boost::phoenix::arg_names::arg1
                                ));
                
                    return oss.str().size();
                }
                
                template <typename Archive = boost::archive::binary_oarchive, typename... Data>
                void benchmark(Data const&... data)
                {
                    std::cout << __PRETTY_FUNCTION__ << ":	" << archive_size<Archive>(data...) << "
                ";
                }
                
                struct Base {
                    boost::array<double, 1000> data;
                    virtual ~Base() {}
                
                  private:
                    friend class boost::serialization::access;
                    template <typename Archive> void serialize(Archive& ar, unsigned /*version*/) {
                        ar & BOOST_SERIALIZATION_NVP(data);
                    }
                };
                
                struct Derived : Base {
                    std::string x;
                    Derived() : x(1000, '') { }
                
                  private:
                    friend class boost::serialization::access;
                    template <typename Archive> void serialize(Archive& ar, unsigned /*version*/) {
                        ar & boost::serialization::make_nvp("base", boost::serialization::base_object<Base>(*this));
                        ar & BOOST_SERIALIZATION_NVP(x);
                    }
                };
                

                测试驱动程序:

                template <typename Archive> 
                void some_scenarios()
                {
                    benchmark<Archive>(std::vector<char>(1000));
                    benchmark<Archive>(boost::make_shared<std::vector<char>>(1000));
                    benchmark<Archive>(3.14f, 42, 42ull, "hello world");
                    benchmark<Archive>(boost::make_shared<Base>());
                    benchmark<Archive>(boost::make_shared<Derived>());
                }
                
                int main()
                {
                    some_scenarios<boost::archive::binary_oarchive>();
                    some_scenarios<boost::archive::text_oarchive>();
                    some_scenarios<boost::archive::xml_oarchive>();
                }
                

                我的 64 位 Ubuntu 和 Boost 1.55 的输出:

                The output on my 64-bit Ubuntu with Boost 1.55:

                void benchmark(const Data& ...) [with Archive = boost::archive::binary_oarchive; Data = {std::vector<char, std::allocator<char> >}]:    1052
                void benchmark(const Data& ...) [with Archive = boost::archive::binary_oarchive; Data = {boost::shared_ptr<std::vector<char, std::allocator<char> > >}]:    1059
                void benchmark(const Data& ...) [with Archive = boost::archive::binary_oarchive; Data = {float, int, long long unsigned int, char [12]}]:   76
                void benchmark(const Data& ...) [with Archive = boost::archive::binary_oarchive; Data = {boost::shared_ptr<Base>}]: 8069
                void benchmark(const Data& ...) [with Archive = boost::archive::binary_oarchive; Data = {boost::shared_ptr<Derived>}]:  9086
                void benchmark(const Data& ...) [with Archive = boost::archive::text_oarchive; Data = {std::vector<char, std::allocator<char> >}]:  2037
                void benchmark(const Data& ...) [with Archive = boost::archive::text_oarchive; Data = {boost::shared_ptr<std::vector<char, std::allocator<char> > >}]:  2043
                void benchmark(const Data& ...) [with Archive = boost::archive::text_oarchive; Data = {float, int, long long unsigned int, char [12]}]: 92
                void benchmark(const Data& ...) [with Archive = boost::archive::text_oarchive; Data = {boost::shared_ptr<Base>}]:   2049
                void benchmark(const Data& ...) [with Archive = boost::archive::text_oarchive; Data = {boost::shared_ptr<Derived>}]:    3083
                void benchmark(const Data& ...) [with Archive = boost::archive::xml_oarchive; Data = {std::vector<char, std::allocator<char> >}]:   16235
                void benchmark(const Data& ...) [with Archive = boost::archive::xml_oarchive; Data = {boost::shared_ptr<std::vector<char, std::allocator<char> > >}]:   17307
                void benchmark(const Data& ...) [with Archive = boost::archive::xml_oarchive; Data = {float, int, long long unsigned int, char [12]}]:  436
                void benchmark(const Data& ...) [with Archive = boost::archive::xml_oarchive; Data = {boost::shared_ptr<Base>}]:    19393
                void benchmark(const Data& ...) [with Archive = boost::archive::xml_oarchive; Data = {boost::shared_ptr<Derived>}]: 21508
                

                如你所见,

                • XML 的开销相当大
                • 对于二进制文件,对于许多不同(例如多态)小类型元素的小档案来说,开销变得很大

                这篇关于提升 C++ 序列化开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
              • <legend id='yn4mc'><style id='yn4mc'><dir id='yn4mc'><q id='yn4mc'></q></dir></style></legend>

                1. <tfoot id='yn4mc'></tfoot>
                    <tbody id='yn4mc'></tbody>
                  • <bdo id='yn4mc'></bdo><ul id='yn4mc'></ul>
                      <i id='yn4mc'><tr id='yn4mc'><dt id='yn4mc'><q id='yn4mc'><span id='yn4mc'><b id='yn4mc'><form id='yn4mc'><ins id='yn4mc'></ins><ul id='yn4mc'></ul><sub id='yn4mc'></sub></form><legend id='yn4mc'></legend><bdo id='yn4mc'><pre id='yn4mc'><center id='yn4mc'></center></pre></bdo></b><th id='yn4mc'></th></span></q></dt></tr></i><div id='yn4mc'><tfoot id='yn4mc'></tfoot><dl id='yn4mc'><fieldset id='yn4mc'></fieldset></dl></div>

                      <small id='yn4mc'></small><noframes id='yn4mc'>