개체 풀은 포인터와 개체만 관리하는 데 사용됩니다.

13435 단어 개체 풀
개체 풀은 포인터와 개체를 관리하는 데 사용되기 때문에 효율적이지 않다
#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로 유지보수하는 대상 탱크는 이미 누드 바늘 관리 성능과 비슷하다.

좋은 웹페이지 즐겨찾기