Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1776077
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: C/C++

2011-09-13 22:55:25

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <iostream>

  5. using namespace std;

  6. class MemPool
  7. {
  8.   public:
  9.      MemPool();
  10.      ~MemPool();
  11.      void* Alloc();
  12.      void Free(void* p);
  13.      
  14.   private:
  15.       const static int blockSize = 1024;
  16.       const static int itemSize = 16;
  17.       
  18.       struct ItemNode
  19.       {
  20.              ItemNode *next;
  21.              char data[itemSize - sizeof(ItemNode*)];
  22.       };
  23.       
  24.       struct BlockNode
  25.       {
  26.              BlockNode *next;
  27.              ItemNode data[blockSize/itemSize];
  28.       };
  29.       
  30.       ItemNode *freeItemNodeHead;
  31.       BlockNode *memBlockNodeHead;
  32. };

  33. MemPool::MemPool():memBlockNodeHead(NULL), freeItemNodeHead(NULL)
  34. {
  35. }

  36. MemPool::~MemPool()
  37. {    
  38.     BlockNode *temp = memBlockNodeHead;
  39.      while(temp)
  40.      {
  41.         memBlockNodeHead = memBlockNodeHead->next;
  42.         delete temp;
  43.         temp = memBlockNodeHead;
  44.     }
  45. }

  46. void* MemPool::Alloc()
  47. {
  48.       if(freeItemNodeHead == NULL)
  49.       {
  50.          BlockNode *tempBlockNode = new BlockNode;
  51.          if(tempBlockNode == NULL)
  52.          {
  53.                    perror("allocate memory error!\n");
  54.                    exit(1);
  55.          }
  56.         
  57.          if(memBlockNodeHead == NULL)
  58.          {
  59.                   memBlockNodeHead = tempBlockNode;
  60.                   memBlockNodeHead->next = NULL;
  61.          }
  62.          else
  63.          {
  64.                   tempBlockNode->next = memBlockNodeHead;
  65.                   memBlockNodeHead = tempBlockNode;
  66.          }
  67.         
  68.          freeItemNodeHead = &(memBlockNodeHead->data[0]);
  69.         
  70.          int i = 0;
  71.          while(i < blockSize/itemSize -1)
  72.          {
  73.                   tempBlockNode->data[i].next = &(tempBlockNode->data[i+1]);
  74.                   ++i;
  75.          }
  76.         
  77.          tempBlockNode->data[i].next = NULL;
  78.      }
  79.     
  80.      ItemNode *allocItemNode = freeItemNodeHead;
  81.      freeItemNodeHead = freeItemNodeHead->next;
  82.     
  83.      return allocItemNode;
  84. }

  85. void MemPool::Free(void* p)
  86. {
  87.       ItemNode *tempItemNode = static_cast<ItemNode*>(p);
  88.       tempItemNode->next = freeItemNodeHead;
  89.       freeItemNodeHead = tempItemNode;
  90. }

  91. int main()
  92. {
  93.      MemPool pool;
  94.      char *p = NULL;
  95.      
  96.      clock_t start, finish;
  97.      
  98.      double duration;
  99.      
  100.      long i = 0;
  101.      
  102.      start = clock();
  103.      
  104.      while(i < 10000000)
  105.      {
  106.              p = static_cast<char*>(pool.Alloc());
  107.              //p = new char[16];
  108.              ++i;
  109.              pool.Free(p);
  110.              //delete p;
  111.     }
  112.     
  113.     finish = clock();
  114.     
  115.     duration = (double)(finish - start)/CLOCKS_PER_SEC;
  116.     
  117.     cout << "total use " << duration << " seconds" << endl;
  118.     
  119.     system("pause");
  120.     
  121.     return 0;
  122. }
 
使用内存池的结果是:
total use 0.245 seconds
使用new,delete操作的结果是:
total use 7.256 seconds
 
结果相差近30倍,可见使用内存池对系统的性能有很大的提高
 

MemPool实现源码参见:http://blog.csdn.net/zhangzhenghe/article/details/5437487

阅读(1590) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~