简单的map性能评测

来源:互联网 发布:斗鱼 诸葛网络 编辑:程序博客网 时间:2024/05/16 10:05

     map是编程中常用的数据结构之一,map在检索的时候的时间复杂度一般为logN,下面这个简单的测试程序能确实证实这一点。当二维数组的大小为6400时,二者的速度相差为15倍左右。

     以下为测试代码:

  1. /************************************************************** 
  2. *    
  3. *   Author: Chaos Lee 
  4. *   Date: 2012-05-14 
  5. *   Description: By traverse a big array, we can draw the  
  6. *           draw the conclusion that what speedup 
  7. *           we can achieve just replacing a simple 
  8. *           data structure. 
  9. ***************************************************************/ 
  10. #include<map> 
  11. #include<iostream> 
  12. #include<sys/time.h> 
  13.  
  14. using namespace std; 
  15.  
  16. class Integer 
  17. public
  18.     Integer(int i):_i(i){} 
  19.     void set(int i){_i = i;} 
  20.     int get(){return _i;} 
  21. private: 
  22.     int _i; 
  23. }; 
  24.  
  25.  
  26. #define M 800 
  27. #define N 8 
  28. #define OFFSET 1000 
  29. int main(int argc,char *argv[]) 
  30.     map<int,Integer *> _map; 
  31.     Integer *** ptr = new Integer**[M]; 
  32.     for(int i=0;i<M;i++) 
  33.     { 
  34.         ptr[i] = new Integer*[N]; 
  35.         for(int j=0;j<N;j++) 
  36.         { 
  37.             ptr[i][j] = new Integer(i*N+j); 
  38.             _map.insert(pair<int,Integer*>(i*OFFSET+j,ptr[i][j])); 
  39.         } 
  40.     } 
  41.     struct timeval start,end
  42.     gettimeofday(&start,NULL); 
  43.     for(int i=0;i<M;i++) 
  44.     { 
  45.         for(int j=0;j<N;j++) 
  46.         { 
  47.             ptr[i][j]->set(i*N+j+1); 
  48.         } 
  49.     } 
  50.     gettimeofday(&end,NULL); 
  51.     unsigned long elapsed1 = (end.tv_sec - start.tv_sec )* 1000000 + end.tv_usec - start.tv_usec; 
  52.     cout<<elapsed1<<endl; 
  53.     gettimeofday(&start,NULL); 
  54.     for(int i=0;i<M;i++) 
  55.     { 
  56.         for(int j=0;j<N;j++) 
  57.         { 
  58.             map<int,Integer*>::iterator iter = _map.find(i*OFFSET + j); 
  59.             if(iter == _map.end()) 
  60.             { 
  61.                 cout<<"error.\n"<<endl; 
  62.                 exit(-1); 
  63.             } 
  64.             iter->second->set(i*N+j); 
  65.         } 
  66.     } 
  67.     gettimeofday(&end,NULL); 
  68.     unsigned long elapsed2 = (end.tv_sec - start.tv_sec )* 1000000 + end.tv_usec - start.tv_usec; 
  69.     cout<<elapsed2<<endl; 
  70.     cout<<"speedup ratio is "<<(float)elapsed2 / elapsed1<<endl; 
  71.     return 0; 

    接下来为测试结果:

     

  1. [lichao@sg01 Performance]$ ./test 
  2. 162 
  3. 2988 
  4. speedup ratio is 18.4444 
  5. [lichao@sg01 Performance]$ ./test 
  6. 136 
  7. 2982 
  8. speedup ratio is 21.9265 
  9. [lichao@sg01 Performance]$ ./test 
  10. 162 
  11. 2966 
  12. speedup ratio is 18.3086 
  13. [lichao@sg01 Performance]$ ./test 
  14. 151 
  15. 3047 
  16. speedup ratio is 20.1788 
  17. [lichao@sg01 Performance]$ ./test 
  18. 193 
  19. 2953 
  20. speedup ratio is 15.3005 
  21. [lichao@sg01 Performance]$ ./test 
  22. 149 
  23. 2950 
  24. speedup ratio is 19.7987 

 

本文出自 “相信并热爱着” 博客,请务必保留此出处http://hipercomer.blog.51cto.com/4415661/863347