用指针Vs增强快速池分配器。宾语

时间:2016-10-25 17:08:12

标签: c++ boost boost-thread

我试图测量加速快速池分配器在std:queue Vs中插入一百万个对象的速度有多快。在std :: queue中插入相同对象的一百万个指针。我希望插入指针要快得多,因为在调用queue.push()方法时插入对象涉及更多的复制。但是当我运行以下代码时,我看到相反的结果:

 struct DepthData
{
    const char*     _symbol;
    double          _price;
    bool            _isAdd;
};

struct QueueData
{
    enum eQueueData { DEPTH, EXECUTION };

    int*        _strategy;
    eQueueData  _eQueueData;
    union
    {
        DepthData _d;
        // Add more later
    }   _type;

};

int main(int argc, char *argv[])
{

    std::cout<< " " << sizeof(QueueData) << " " << sizeof(QueueData*);

    typedef boost::fast_pool_allocator<QueueData, boost::default_user_allocator_new_delete, boost::details::pool::default_mutex, 2000000*sizeof(QueueData), 0> FastAllocT;
    std::queue<QueueData, std::deque<QueueData, FastAllocT> > cq2;
    struct timespec startime2;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &startime2);
    for (double i; i< 1000000; ++i)
    {
        QueueData q2;
        cq2.push(q2);
    }
    struct timespec endTime2;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &endTime2);
    std::cout<<" "<<endTime2.tv_nsec-startime2.tv_nsec;
    boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(QueueData)>::purge_memory();

    typedef boost::fast_pool_allocator<QueueData*, boost::default_user_allocator_new_delete, boost::details::pool::default_mutex, 2000000*sizeof(QueueData*), 0> FastAllocT2;
    std::queue<QueueData*, std::deque<QueueData*, FastAllocT2> > cq3;
    struct timespec startime3;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &startime3);
    for (double i; i< 1000000; ++i)
    {
        QueueData* obj = NULL;
        cq3.push(obj);
    }
    struct timespec endTime3;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &endTime3);
    std::cout<<" "<<endTime3.tv_nsec-startime3.tv_nsec;
    boost::singleton_pool<boost::fast_pool_allocator_tag, sizeof(QueueData*)>::purge_memory();

    return 0;
}

输出如下: 40 8 8957929 14162703

第3号没有。 (即8957929)是插入一百万个物体和第四个物体的时间。 (即14162703)是插入一百万个指针的时间。我无法解释为什么插入指针需要更长的时间(特别是我的只是NULL指针!)?

我在CentOS上运行它并使用gcc版本4.8.5使用CMAKE Release版本类型构建代码。

0 个答案:

没有答案
相关问题