对象池只是管理指针和对象用的

对象池只是管理指针和对象用的,并不高效

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <map>
#include <pthread.h>
#include <list>
#include <vector>
#include <queue>

#include "CommonStruct.h"
#include "InputMonitor.h"
#include "OutPutMonitor.h"
#include "MessageBlock.h"
#include "MessageBlockCache.h"
#include "BufQueue.h"
#include "TimeKit.h"
#include "DuplexQueue.h"
#include "ObjectPool.h"


//DuplexList* _recv_net_msg_queue = NULL;
//DuplexList* _send_net_msg_queue = NULL;


DuplexQueue _queue;

void* process(void* arg)
{

    int i=0;
    while(true)
    {
        int *j = new int();
        *j = i;

        _queue.append((void *)j);
        i ++;
        if(i % 40 == 0)
        {
            usleep(2);
        }
    }
    return NULL;
}

void functions()
{

    std::map<int,int> v;

    for(int i=0;i<20;i++)
    {
        v[i] = i;
    }
    int j=0;

    for(std::map<int,int>::iterator iter = v.begin();
            iter != v.end();
            iter ++)
    {
        v.find(iter->first);
    }
}

class TestObject
{
public:
    TestObject()
    {
        this->i = 0;
        this->_test.clear();
    }
    ~TestObject()
    {

    }

private:
    int i;
    list<int> _test;
    char cc[100];
};

template <class CObject>
class CObjectPool
{
public:
    CObjectPool()
    {
        CObject *c = NULL;
        for(int i=0;i<100;i++)
        {
            c = new CObject();
            this->_list.push_back(c);
        }
    }
    ~CObjectPool()
    {

    }

    void pop(CObject *&C)
    {
        if(!this->_list.empty())
        {
            C = this->_list.back();
            this->_list.pop_back();
        }
        else
        {
            for(int i = 0 ; i < 100 ; i++)
            {
                CObject* c = new CObject();
                this->_list.push_back(c);
            }
            pop(C);
        }
    }

    void push(CObject *c)
    {
        this->_list.push_back(c);
    }

private:
    std::vector<CObject*> _list;
};

template <class CObject>
class CObjectPoolQ
{
public:
    CObjectPoolQ()
    {
        CObject *c = NULL;
        for(int i=0;i<100;i++)
        {
            c = new CObject();
            this->_list.push(c);
        }
    }
    ~CObjectPoolQ()
    {

    }

    void pop(CObject *&C)
    {
        if(!this->_list.empty())
        {
            C = this->_list.front();
            this->_list.pop();
        }
        else
        {
            for(int i = 0 ; i < 100 ; i++)
            {
                CObject* c = new CObject();
                this->_list.push(c);
            }
            pop(C);
        }
    }

    void push(CObject *c)
    {
        this->_list.push(c);
    }

private:
    std::queue<CObject*> _list;
};



int main(int argc,char* argv[])
{
    CObjectPoolQ<TestObject> obj;
    long long int start = TimeKit::get_tick();

    std::list<TestObject*> _list;

    for(int i=0;i<1000000;i++)
    {
        TestObject* p;
//        obj.pop(p);
        p = new TestObject();
        _list.push_front(p);
    }

    for(std::list<TestObject*>::iterator iter = _list.begin() ; iter != _list.end() ; iter ++)
    {
//        obj.push(*iter);
        delete *iter;
    }

    long long int end = TimeKit::get_tick();

    printf("%ld",(end - start));

    return 0;
}

有兴趣的同学可以测试下上面的代码!

stl的vector 和 queue 相对高效,比list快不少,用vector和queue 维护的对象池,已经和裸指针管理性能相当。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值