개체 풀은 포인터와 개체만 관리하는 데 사용됩니다.
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로 유지보수하는 대상 탱크는 이미 누드 바늘 관리 성능과 비슷하다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
CocosCreator 객체 풀 사용 방법우리가 노드를 만들어야 할 때, 대상 탱크에 노드를 신청하고, 대상 탱크에 사용할 수 있는 노드가 있으면, 노드를 사용자에게 되돌려주고, 사용자는node를 통해.add Child는 이 새 노드를 장면 노드 트리에 추...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.