如何使用自定义库的boost库进行性能测试 我需要做一个用C++编写的库的性能测试。图书馆由几组结构组成。我已经对这些类进行了序列化测试,但不确定如何对这些类进行性能测试。下面是库中的一个结构示例 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(); } struct X { 公众: INTP; 双q; X(); ~X(); } 结构 { 浮动m; 双n; Y(); ~Y(); } 结构Z { 公众: std::map-Xtype; std::map-Ytype; int i; 字符串名; Z(); ~Z(); }

如何使用自定义库的boost库进行性能测试 我需要做一个用C++编写的库的性能测试。图书馆由几组结构组成。我已经对这些类进行了序列化测试,但不确定如何对这些类进行性能测试。下面是库中的一个结构示例 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(); } struct X { 公众: INTP; 双q; X(); ~X(); } 结构 { 浮动m; 双n; Y(); ~Y(); } 结构Z { 公众: std::map-Xtype; std::map-Ytype; int i; 字符串名; Z(); ~Z(); },c++,serialization,boost,performance-testing,boost-serialization,C++,Serialization,Boost,Performance Testing,Boost Serialization,如果提供了任何示例,那么它将非常好。好的,所以我在类型中添加了序列化(为什么省略它?) 交互式绘图: 测试夹具实际上要做更多的工作,定义如下: #include <boost/random.hpp> // for test data #include <boost/bind.hpp> #include <boost/make_shared.hpp> #include <algorithm> Z const& fixture() {

如果提供了任何示例,那么它将非常好。

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

交互式绘图:


测试夹具实际上要做更多的工作,定义如下:

#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;
}
#包含//用于测试数据
#包括
#包括

#include

使用@CaptainObvlious-只需要在单元测试中使用boost,这甚至不是一句话,@user3295725。为什么突然提到单元测试?@sehe--对不起,我错提了。。这应该只是测试。。
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
    });
})
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
#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;
}