c – 提高字符串的分配性能

c – 提高字符串的分配性能,第1张

概述我将 Java GC测试程序移植到C(参见下面的代码)以及 Python. Java和 Python的性能远远超过C,我认为这是因为每次都需要完成对new的所有调用.我尝试过使用Boost的fast_pool_allocator,但实际上性能从700ms提高到1200ms.我使用分配器是错误的,还是我应该做的其他事情? 编辑:使用g -O3 -march = native –std = c 11 我将 Java GC测试程序移植到C(参见下面的代码)以及 Python. Java和 Python的性能远远超过C,我认为这是因为每次都需要完成对new的所有调用.我尝试过使用Boost的fast_pool_allocator,但实际上性能从700ms提高到1200ms.我使用分配器是错误的,还是我应该做的其他事情?

编辑:使用g -O3 -march = native –std = c 11 garbage.cpp -lboost_system编译. g是版本4.8.1
Python中的一次迭代大约需要300ms,Java大约需要50ms. std :: allocator提供大约700ms,boost :: fast_pool_allocator提供大约1200ms.

#include <string>#include <vector>#include <chrono>#include <List>#include <iostream>#include <boost/pool/pool_alloc.hpp>#include <memory>//#include <gc/gc_allocator.h>using namespace std;#include <sstream>typedef boost::fast_pool_allocator<char> c_allocator;//typedef std::allocator<char> c_allocator;typedef basic_string<char,char_traits<char>,c_allocator> pool_string;namespace patch {    template <typename T> pool_string to_string(const T& in) {        std::basic_stringstream<char,c_allocator> stm;        stm << in;        return stm.str();    }}#include "mytime.hpp"class Garbage {public:    vector<pool_string> outer;    vector<pool_string> old;    const int nThreads = 1;    //static auto time = chrono::high_resolution_clock();    voID go() {//        outer.resize(1000000);        //old.reserve(1000000);        auto tt = mytime::msecs();        for (int i = 0; i < 10; ++i) {            if (i % 100 == 0) {                cout << "DOING AN olD" << endl;                doold();                tt = mytime::msecs();            }            for (int j = 0; j < 1000000/nThreads; ++j)                outer.push_back(patch::to_string(j));            outer.clear();            auto t = mytime::msecs();            cout << (t - tt) << endl;            tt = t;        }    }    voID doold() {        old.clear();        for (int i = 0; i < 1000000/nThreads; ++i)            old.push_back(patch::to_string(i));    }};int main() {    Garbage().go();}
解决方法 问题是你每次都使用一个新的字符串流来转换一个整数.

修理它:

namespace patch {    template <typename T> pool_string to_string(const T& in) {        return boost::lexical_cast<pool_string>(in);    }}

现在的时间是:

DOING AN olD0.1754620.06700850.06699260.06879690.06925180.06693180.06691960.06691870.06689620.0669185real    0m0.801suser    0m0.784ssys 0m0.016s

见它Live On Coliru

完整代码供参考:

#include <boost/pool/pool_alloc.hpp>#include <chrono>#include <iostream>#include <List>#include <memory>#include <sstream>#include <string>#include <vector>#include <boost/lexical_cast.hpp>//#include <gc/gc_allocator.h>using string = std::string;namespace patch {    template <typename T> string to_string(const T& in) {        return boost::lexical_cast<string>(in);    }}class Timer{    typedef std::chrono::high_resolution_clock clock;    clock::time_point _start;  public:    Timer() { reset(); }    voID reset() { _start = Now(); }    double elapsed()    {        using namespace std::chrono;        auto e = Now() - _start;        return duration_cast<nanoseconds>(e).count()*1.0e-9;    }    clock::time_point Now()    {        return clock::Now();    }};class Garbage {    public:        std::vector<string> outer;        std::vector<string> old;        const int nThreads = 1;        voID go() {            outer.resize(1000000);            //old.reserve(1000000);            Timer timer;            for (int i = 0; i < 10; ++i) {                if (i % 100 == 0) {                    std::cout << "DOING AN olD" << std::endl;                    doold();                }                for (int j = 0; j < 1000000/nThreads; ++j)                    outer.push_back(patch::to_string(j));                outer.clear();                std::cout << timer.elapsed() << std::endl;                timer.reset();            }        }        voID doold() {            old.clear();            for (int i = 0; i < 1000000/nThreads; ++i)                old.push_back(patch::to_string(i));        }};int main() {    Garbage().go();}
总结

以上是内存溢出为你收集整理的c – 提高字符串的分配性能全部内容,希望文章能够帮你解决c – 提高字符串的分配性能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1215960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存