使用boost singleton_pool的自定义分配比默认慢 [英] Custom allocation using boost singleton_pool slower than default

查看:893
本文介绍了使用boost singleton_pool的自定义分配比默认慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为类MyOrder编写了自定义运算符new和运算符删除。我使用boost :: singleton池分配内存。这里是程序测试性能,

I wrote custom operator new and operator delete for the class MyOrder. I am allocating memory using boost::singleton pool. Here is the program testing the performance,

#include <boost/pool/singleton_pool.hpp>
#include <boost/progress.hpp>
#include <iostream>
#include <new>
#include <vector>


class MyOrder{
    std::vector<int> v1_;
    std::vector<double> v2_;

    std::string s1_;
    std::string s2_;

public:
    MyOrder(std::string s1, std::string s2): s1_(s1), s2_(s2) {}

    ~MyOrder(){}

    static void * operator new(size_t size); 
    static void operator delete(void * rawMemory) throw();
};

struct MyOrderTag{};
typedef boost::singleton_pool<MyOrderTag, sizeof(MyOrder)> MyOrderPool; 

void* MyOrder:: operator new(size_t size)
{
    if (size != sizeof(MyOrder)) 
        return ::operator new(size);

    while(true){
        void * ptr = MyOrderPool::malloc();
        if (ptr != NULL) return ptr;

        std::new_handler globalNewHandler = std::set_new_handler(0);
        std::set_new_handler(globalNewHandler);

        if(globalNewHandler)  globalNewHandler();
        else throw std::bad_alloc();

    }
}

void MyOrder::operator delete(void * rawMemory) throw()
{
    if(rawMemory == 0) return; 
    MyOrderPool::free(rawMemory);
}

int main()
{
    MyOrder* mo = NULL; 
    std::vector<MyOrder*> v;
    v.reserve(100000);

    boost::progress_timer howlong;
    for(int i = 0; i< 100000; ++i)
    {
        mo = new MyOrder("Sanket", "Sharma");
        v.push_back(mo);
    }

    for (std::vector<MyOrder*>::const_iterator it = v.begin(); it != v.end(); ++it)
    {
        delete *it;
    }
    return 0;
}

我使用-O2标志编译了上述程序,并在我的Macbook上运行2.26 GHz Intel Core 2 Duo,花了0.16秒。然后我注释掉了我已声明和定义自定义运算符new和操作员删除,重新编译与-O2标志和运行在同一台机器上花了0.13秒的行。

I compiled the above program using -O2 flag and ran on my Macbook with 2.26 GHz Intel Core 2 Duo and it took 0.16 seconds. Then I commented off the lines where I have declared and defined the custom operator new and operator delete, recompiled with -O2 flags and ran on the same machine it took 0.13 seconds.

使用singleton_pool为相同大小的对象分配和释放内存应该会加快速度。为什么它使它慢?

Allocating and deallocating memory using singleton_pool for objects of same size should speed it up. Why is it making it slow? Or is the overhead of creating a pool nullifying the performance benefit gained in this small program?

更新:

我用int和double替换了两个std :: string变量,这次在3.0 GHZ AMD Phenom(tm)II X4 945处理器上运行这两个程序,每个都有100000000(即1000次)迭代。使用自定义内存分配的方法需要3.2秒,而使用默认内存分配的方法需要8.26秒。所以这次定制内存分配赢了。

I replaced the two std::string variables with an int and a double and this time ran the two programs with 100000000 (ie 1000 times before) iterations each on a 3.0 GHZ AMD Phenom(tm) II X4 945 Processor. The one using custom memory allocation takes 3.2 seconds while the one using default memory allocation takes 8.26 seconds. So this time custom memory allocation wins.

推荐答案

我认为你的数字没有意义。如果你只检查一次运行时,你发现 0.13 vs 0.16 秒比这完全没有意义,

I think your numbers are meaningless. If you only checked the runtime once, and you found 0.13 vs 0.16 seconds than that is entirely meaningless, and dominated by overhead.

您必须运行要测试数千次的代码段,然后比较数据以排除开销。

You must run the snippet you want to test thousands of times and then compare the data to rule out overhead.

没有, 0.03 秒差异可以很容易地解释为你的过程被切断等等。

No really, that 0.03 seconds difference can easily be explained by your process getting switched out, etc.

这篇关于使用boost singleton_pool的自定义分配比默认慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆