• <legend id='EHQnY'><style id='EHQnY'><dir id='EHQnY'><q id='EHQnY'></q></dir></style></legend>
    1. <small id='EHQnY'></small><noframes id='EHQnY'>

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

        如何使用自定义库的 boost 库进行性能测试

        时间:2023-06-04

          <tbody id='2o4Hp'></tbody>
        <legend id='2o4Hp'><style id='2o4Hp'><dir id='2o4Hp'><q id='2o4Hp'></q></dir></style></legend>

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

            <small id='2o4Hp'></small><noframes id='2o4Hp'>

                  本文介绍了如何使用自定义库的 boost 库进行性能测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要对用 C++ 编写的库进行性能测试.该库由几组结构组成.我已经对这些类进行了序列化测试,但不确定如何对这些类进行性能测试.下面是库中结构的示例

                  struct X{上市:内部 p;双 q;X();~X();}结构体{浮动米;双 n;Y();~Y();}结构体{上市:std::map<std::string,boost::shared_ptr<X>>X型;std::map>类型;国际我;字符串名称;Z();~Z();}

                  如果提供任何示例,那将非常好.

                  解决方案

                  好的,所以我在类型中添加了序列化(你为什么省略它?)

                  struct X{内部 p;双 q;私人的:朋友 boost::serialization::access;模板 无效序列化(Ar& ar,无符号){和BOOST_SERIALIZATION_NVP(p);和BOOST_SERIALIZATION_NVP(q);}};结构体{浮动米;双 n;私人的:朋友 boost::serialization::access;模板 无效序列化(Ar& ar,无符号){和BOOST_SERIALIZATION_NVP(m);和BOOST_SERIALIZATION_NVP(n);}};结构体{std::map>X型;std::map>类型;国际我;std::string 名称;私人的:朋友 boost::serialization::access;模板 无效序列化(Ar& ar,无符号){和BOOST_SERIALIZATION_NVP(i);和BOOST_SERIALIZATION_NVP(名称);和BOOST_SERIALIZATION_NVP(Xtype);和BOOST_SERIALIZATION_NVP(Ytype);}};

                  现在,使用

                  <小时>

                  测试fixture其实做的工作比较多,定义如下:

                  #include //用于测试数据#include #include #include <算法>Z常量&夹具(){静态 Z const z = [] {Z Z;boost::random::mt19937 引擎;auto fgen = boost::bind(boost::random::uniform_real_distribution(), engine);auto dgen = boost::bind(boost::random::uniform_real_distribution(), engine);auto cgen = boost::bind(boost::random::uniform_int_distribution('a', 'z'), engine);auto igen = boost::bind(boost::random::uniform_int_distribution(), engine);auto sgen = [&] (int maxlen) { std::string s;std::generate_n(back_inserter(s), igen() % maxlen, cgen);返回 s;};std::generate_n(inserter(z.Ytype, z.Ytype.end()), 1000, [&] {auto py = boost::make_shared();py->m = fgen();py->n = dgen();返回 std::make_pair(sgen(32), py);});std::generate_n(inserter(z.Xtype, z.Xtype.end()), 3000, [&] {auto px = boost::make_shared();px->p = igen();px->q = dgen();返回 std::make_pair(sgen(32), px);});z.i = igen();z.name = sgen(8);返回 z;}();返回 z;}

                  完整代码清单

                  Coliru

                  I need to do performance testing of a library written in c++. The library consist of few sets of structures. I have already done the serialization test for these class but not sure how to do perfomance test for these . Below is sample of a struct in library

                  struct X
                  {
                     public:
                        int p;
                        double q;
                  
                        X();
                       ~X();
                  }
                  
                  struct Y
                  {
                      float m;
                      double n;
                  
                       Y();
                      ~Y();
                  }
                  
                  struct Z
                  {
                  public:
                     std::map<std::string,boost::shared_ptr<X>> Xtype;
                     std::map<std::string,boost::shared_ptr<Y>> Ytype; 
                  
                     int i;
                     string name; 
                  
                     Z();
                    ~Z();
                  
                  }
                  

                  If any example is provided then it will be really good.

                  解决方案

                  Okay, so I added serialization to the types (why did you leave it out?)

                  struct X
                  {
                      int p;
                      double q;
                    private: 
                      friend boost::serialization::access;
                      template <typename Ar>
                          void serialize(Ar& ar, unsigned) {
                              ar & BOOST_SERIALIZATION_NVP(p);
                              ar & BOOST_SERIALIZATION_NVP(q);
                          }
                  };
                  
                  struct Y
                  {
                      float m;
                      double n;
                    private: 
                      friend boost::serialization::access;
                      template <typename Ar>
                          void serialize(Ar& ar, unsigned) {
                              ar & BOOST_SERIALIZATION_NVP(m);
                              ar & BOOST_SERIALIZATION_NVP(n);
                          }
                  };
                  
                  struct Z
                  {
                     std::map<std::string, boost::shared_ptr<X>> Xtype;
                     std::map<std::string, boost::shared_ptr<Y>> Ytype; 
                  
                     int i;
                     std::string name; 
                    private: 
                      friend boost::serialization::access;
                      template <typename Ar>
                          void serialize(Ar& ar, unsigned) {
                              ar & BOOST_SERIALIZATION_NVP(i);
                              ar & BOOST_SERIALIZATION_NVP(name);
                              ar & BOOST_SERIALIZATION_NVP(Xtype);
                              ar & BOOST_SERIALIZATION_NVP(Ytype);
                          }
                  };
                  

                  And now, using the Nonius benchmarking mini-framework, write the following benchmarks:

                  Z const& fixture(); // forward
                  
                  #include <nonius/main.h++>
                  #include <sstream>
                  
                  NONIUS_BENCHMARK("text archive", [](nonius::chronometer meter) {
                      auto const& z = fixture();
                      meter.measure([&](int /*i*/) { 
                          std::stringstream ss;
                          boost::archive::text_oarchive oa(ss);
                          oa << z;
                  
                          Z clone;
                          boost::archive::text_iarchive ia(ss);
                          ia >> clone;
                  
                          return ss.str().size(); // something observable to thwart the overly smart optimizer
                      });
                  })
                  
                  NONIUS_BENCHMARK("binary archive", [](nonius::chronometer meter) {
                      auto const& z = fixture();
                      meter.measure([&](int /*i*/) { 
                          std::stringstream ss;
                          boost::archive::binary_oarchive oa(ss);
                          oa << z;
                  
                          Z clone;
                          boost::archive::binary_iarchive ia(ss);
                          ia >> clone;
                  
                          return ss.str().size(); // something observable to thwart the overly smart optimizer
                      });
                  })
                  
                  NONIUS_BENCHMARK("xml archive", [](nonius::chronometer meter) {
                      auto const& z = fixture();
                      meter.measure([&](int /*i*/) { 
                          std::stringstream ss;
                          boost::archive::xml_oarchive oa(ss);
                          oa << boost::serialization::make_nvp("root", z);
                  
                          Z clone;
                          boost::archive::xml_iarchive ia(ss);
                          ia >> boost::serialization::make_nvp("root", clone);
                  
                          return ss.str().size(); // something observable to thwart the overly smart optimizer
                      });
                  })
                  

                  The raw output is (for a fixture of 1000 random X and 3000 random Y values):

                  text archive
                  mean: 236.069 μs
                  std dev: 2.54923 μs
                  variance is unaffected by outliers
                  
                  binary archive
                  mean: 92.9736 μs
                  std dev: 3.35504 μs
                  variance is moderately inflated by outliers
                  
                  xml archive
                  mean: 786.746 μs
                  std dev: 4.676 μs
                  variance is unaffected by outliers
                  

                  Interactive plot: click here


                  The test fixture is actually a lot more work, and is defined as follows:

                  #include <boost/random.hpp> // for test data
                  #include <boost/bind.hpp>
                  #include <boost/make_shared.hpp>
                  #include <algorithm>
                  
                  Z const& fixture()
                  {
                      static Z const z = [] {
                          Z z;
                  
                          boost::random::mt19937 engine;
                          auto fgen = boost::bind(boost::random::uniform_real_distribution<float>(), engine);
                          auto dgen = boost::bind(boost::random::uniform_real_distribution<double>(), engine);
                          auto cgen = boost::bind(boost::random::uniform_int_distribution<char>('a', 'z'), engine);
                          auto igen = boost::bind(boost::random::uniform_int_distribution<int>(), engine);
                  
                          auto sgen = [&] (int maxlen) { std::string s; std::generate_n(back_inserter(s), igen() % maxlen, cgen); return s; };
                  
                          std::generate_n(inserter(z.Ytype, z.Ytype.end()), 1000, [&] { 
                                  auto py = boost::make_shared<Y>(); 
                                  py->m = fgen();
                                  py->n = dgen();
                                  return std::make_pair(sgen(32), py);
                                  });
                          std::generate_n(inserter(z.Xtype, z.Xtype.end()), 3000, [&] { 
                                  auto px = boost::make_shared<X>(); 
                                  px->p = igen();
                                  px->q = dgen();
                                  return std::make_pair(sgen(32), px);
                                  });
                  
                          z.i    = igen();
                          z.name = sgen(8);
                  
                          return z; 
                      }();
                      return z;
                  }
                  

                  Full Code Listing

                  On Coliru

                  这篇关于如何使用自定义库的 boost 库进行性能测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:获取非侵入式 boost 序列化 C++ 的私有数据成员 下一篇:直接提升序列化到字符数组

                  相关文章

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

                    • <bdo id='hHwlu'></bdo><ul id='hHwlu'></ul>

                      <legend id='hHwlu'><style id='hHwlu'><dir id='hHwlu'><q id='hHwlu'></q></dir></style></legend>
                    1. <small id='hHwlu'></small><noframes id='hHwlu'>

                      <tfoot id='hHwlu'></tfoot>