简单的map性能评测

简介:

   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 

 


本文转自hipercomer 51CTO博客,原文链接:http://blog.51cto.com/hipercomer/863347


相关文章
|
JSON JavaScript API
JS【详解】Map (含Map 和 Object 的区别,Map 的常用 API,Map与Object 的性能对比,Map 的应用场景和不适合的使用场景)
JS【详解】Map (含Map 和 Object 的区别,Map 的常用 API,Map与Object 的性能对比,Map 的应用场景和不适合的使用场景)
1366 0
|
JavaScript 前端开发 测试技术
Map 和 Object 在处理大量数据时性能差异
Map 和 Object 在处理大量数据时性能差异
|
数据可视化 计算机视觉
深入了解平均精度(mAP):通过精确率-召回率曲线评估目标检测性能
平均精度(Average Precision,mAP)是一种常用的用于评估目标检测模型性能的指标。在目标检测任务中,模型需要识别图像中的不同目标,并返回它们的边界框(bounding box)和类别。mAP用于综合考虑模型在不同类别上的准确度和召回率。
2249 0
|
Java
Java Map集合的几种遍历方式与性能对比(包含lambda表达式)
综上所述:第三种遍历方式在数据量非常小时是最好的,第五种遍历方式是最简单粗暴的。
790 0
Java Map集合的几种遍历方式与性能对比(包含lambda表达式)
|
Java 测试技术 索引
list 、set 、map 粗浅性能对比分析
 list 、set 、map 粗浅性能对比分析   不知道有多少同学和我一样,工作五年了还没有仔细看过list、set的源码,一直停留在老师教导的:“LinkedList插入性能比ArrayList好,LinkedList顺序遍历性能比ArrayList好”的世界里。
1583 0
|
安全 Java 数据库连接
让我们讲解一下 Map 集合遍历的方式
我是小假 期待与你的下一次相遇 ~
412 43
|
存储 前端开发 API
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
该文章详细介绍了ES6中Set和Map数据结构的特性和使用方法,并探讨了它们在前端开发中的具体应用,包括如何利用这些数据结构来解决常见的编程问题。
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
|
存储 安全 Java
java集合框架复习----(4)Map、List、set
这篇文章是Java集合框架的复习总结,重点介绍了Map集合的特点和HashMap的使用,以及Collections工具类的使用示例,同时回顾了List、Set和Map集合的概念和特点,以及Collection工具类的作用。
java集合框架复习----(4)Map、List、set