首页 > 其他分享 >Qt QVector和vector以及QMap和map的遍历性能对比

Qt QVector和vector以及QMap和map的遍历性能对比

时间:2024-02-27 19:57:21浏览次数:25  
标签:map end QMap iterator 遍历性 sum start toMSecsSinceEpoch printf

  使用Qt中的容器给C++开发带来很大的便利,而且QVector和QMap等容器扩展的一些成员函数也是很方便的。但是Qt的这些容器和STL库的容器比,效率到底怎么样?我就写了几个简单的遍历的例子,测试了QVector、vector等容器的那些方法效率更高。

测试环境:

系统:windows 10

编译器:MingGW mingw5.3.0

Qt版本:5.9.7

硬件:i7 8代,16G内存

一. 测试QVector和vector的各种遍历的方法:

  1 void CompareQVectorAndVectorTraverse()
  2 {
  3     int size = 100000000;
  4     QVector<int> qv(size, 1);
  5     vector<int> v(size, 1);
  6  
  7     printf("vector size: %d\n", size);
  8 // const_iterator比较
  9     QVector<int>::ConstIterator cqvit;
 10     int sum = 0;
 11     qint64 start = QDateTime::currentDateTime().toMSecsSinceEpoch();
 12     for( cqvit = qv.constBegin(); cqvit != qv.constEnd(); ++cqvit)
 13     {
 14         sum += *cqvit;
 15     }
 16     qint64 end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 17     printf( "qvector ConstIterator %dms\n", end - start );
 18     printf("%d\n", sum);
 19  
 20     QVector<int>::const_iterator cqvit2;
 21     sum = 0;
 22     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
 23     for( cqvit2 = qv.cbegin(); cqvit2 != qv.cend(); ++cqvit2)
 24     {
 25         sum += *cqvit2;
 26     }
 27     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 28     printf( "qvector const_iterator %dms\n", end - start );
 29     printf("%d\n", sum);
 30  
 31     vector<int>::const_iterator cvit;
 32     sum = 0;
 33     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
 34     for( cvit = v.cbegin(); cvit != v.cend(); ++cvit )
 35     {
 36         sum += *cvit;
 37     }
 38     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 39     printf( "stl vector const_iterator %dms\n", end - start );
 40     printf("%d\n", sum);
 41  
 42 // iterator比较
 43     QVector<int>::iterator it;
 44     sum = 0;
 45     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
 46     for( it = qv.begin(); it != qv.end(); ++it)
 47     {
 48         sum += *it;
 49     }
 50     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 51     printf( "qvector iterator %dms\n", end - start );
 52     printf("%d\n", sum);
 53  
 54     vector<int>::iterator vit;
 55     sum = 0;
 56     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
 57     for( vit = v.begin(); vit != v.end(); ++vit )
 58     {
 59         sum += *vit;
 60     }
 61     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 62     printf( "stl vector iterator %dms\n", end - start );
 63     printf("%d\n", sum);
 64  
 65 //[]比较
 66     sum = 0;
 67     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
 68     for( int i = 0; i < qv.size(); ++i)
 69     {
 70         sum += qv[i];
 71     }
 72     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 73     printf( "QVector [] %dms\n", end - start );
 74     printf("%d\n", sum);
 75  
 76  
 77     sum = 0;
 78     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
 79     for( int i = 0; i < v.size(); ++i)
 80     {
 81         sum += v[i];
 82     }
 83     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 84     printf( "stl vector [] %dms\n", end - start );
 85     printf("%d\n", sum);
 86  
 87 //.data()比较
 88     sum = 0;
 89     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
 90     int *data = qv.data();
 91     for( int i = 0; i < qv.size(); ++i)
 92     {
 93         sum += data[i];
 94     }
 95     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
 96     printf( "QVector .data() %dms\n", end - start );
 97     printf("%d\n", sum);
 98  
 99     sum = 0;
100     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
101     data = v.data();
102     for( int i = 0; i < v.size(); ++i)
103     {
104         sum += data[i];
105     }
106     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
107     printf( "stl vector .data() %dms\n", end - start );
108     printf("%d\n", sum);
109  
110 //:比较
111     sum = 0;
112     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
113     for( int val : qv )
114     {
115         sum += val;
116     }
117     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
118     printf( "QVector : %dms\n", end - start );
119     printf("%d\n", sum);
120  
121     sum = 0;
122     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
123     for( int val : v)
124     {
125         sum += val;
126     }
127     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
128     printf( "stl vector : %dms\n", end - start );
129     printf("%d\n", sum);
130 }

运行结果:

 1 vector size: 100000000
 2 qvector ConstIterator 1252ms
 3 100000000
 4 qvector const_iterator 1254ms
 5 100000000
 6 stl vector const_iterator 1318ms
 7 100000000
 8 qvector iterator 4233ms
 9 100000000
10 stl vector iterator 1319ms
11 100000000
12 QVector [] 4862ms
13 100000000
14 stl vector [] 456ms
15 100000000
16 QVector .data() 275ms
17 100000000
18 stl vector .data() 296ms
19 100000000
20 QVector : 187ms
21 100000000
22 stl vector : 870ms
23 100000000

二. 测试QMap和map的各种遍历的方法:

 1 void CompareQMapAndMapTraverse()
 2 {
 3     int size = 1000000;
 4     qint64 sum = 0;
 5     qint64 start, end;
 6     map<string, int> m;
 7     char *tmps = new char[10];
 8     for( int i = 0; i < size; ++i )
 9     {
10         sprintf( tmps, "%s%d", "a", i);
11         m.insert( make_pair(tmps, i) );
12     }
13     QMap<string, int> qm(m);
14     printf("map size: %d\n", m.size());
15  
16  
17 //iterator比较
18     QMap<string, int>::iterator qmit;
19     sum = 0;
20     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
21     for(qmit = qm.begin(); qmit != qm.end(); ++qmit )
22     {
23         sum += qmit.value();
24     }
25     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
26     printf( "QMap iterator %dms\n", end - start );
27     printf("%d\n", sum);
28  
29     map<string, int>::iterator mit;
30     sum = 0;
31     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
32     for( mit = m.begin(); mit != m.end(); ++mit )
33     {
34         sum += mit->second;
35     }
36     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
37     printf( "stl::map iterator %dms\n", end - start );
38     printf("%d\n", sum);
39  
40 //const_iterator比较
41     QMap<string, int>::const_iterator cqmit;
42     sum = 0;
43     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
44     for(cqmit = qm.cbegin(); cqmit != qm.cend(); ++cqmit )
45     {
46         sum += cqmit.value();
47     }
48     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
49     printf( "QMap const_iterator %dms\n", end - start );
50     printf("%d\n", sum);
51  
52     map<string, int>::const_iterator cmit;
53     sum = 0;
54     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
55     for( cmit = m.cbegin(); cmit != m.cend(); ++cmit )
56     {
57         sum += cmit->second;
58     }
59     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
60     printf( "stl::map const_iterator %dms\n", end - start );
61     printf("%d\n", sum);
62  
63     QMap<string, int>::key_iterator qmkeyit;
64     sum = 0;
65     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
66     for( qmkeyit = qm.keyBegin(); qmkeyit != qm.keyEnd(); ++qmkeyit )
67     {
68         sum += qm[*qmkeyit];
69     }
70     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
71     printf( "QMap key_iterator %dms\n", end - start );
72     printf("%d\n", sum);
73  
74     sum = 0;
75     start = QDateTime::currentDateTime().toMSecsSinceEpoch();
76     for( string k : qm.keys() )
77     {
78         sum += qm[k];
79     }
80     end = QDateTime::currentDateTime().toMSecsSinceEpoch();
81     printf( "QMap .keys %dms\n", end - start );
82     printf("%d\n", sum);
83  
84 }

运行结果:

 1 map size: 1000000
 2 QMap iterator 49ms
 3 1783293664
 4 stl::map iterator 38ms
 5 1783293664
 6 QMap const_iterator 33ms
 7 1783293664
 8 stl::map const_iterator 38ms
 9 1783293664
10 QMap key_iterator 407ms
11 1783293664
12 QMap .keys 551ms
13 1783293664

基本和预期的结果差不多,得出结果是QVector最好用for(T &t : qvT)方式。QMap用iterator比较快,而且const_iterator和iterator差不多。

标签:map,end,QMap,iterator,遍历性,sum,start,toMSecsSinceEpoch,printf
From: https://www.cnblogs.com/ybqjymy/p/18037751

相关文章

  • Qt QVector、QList、QSet和QMap:性能与用途比较
    Qt提供了多种容器类,用于存储和管理数据。其中,QVector、QList、QSet和QMap是最常用的几种。这些容器类在性能和用途方面存在一些差异,选择合适的容器对于提高应用程序的效率和正确性至关重要。下面我们将从以下几个方面对这四种容器进行比较:1.存储方式QVector:动态数组,元素在......
  • 扩展运算符...+map+filter 在嵌套对象数组中的使用
    参考文档:使用基于嵌套值的数组过滤对象数组:https://segmentfault.com/q/1010000042989861js扩展运算符(...)的用法 :https://www.cnblogs.com/caihongmin/p/16395573.html对象的扩展运算符:https://blog.csdn.net/weixin_42265852/article/details/88739525Vue判断对象中......
  • List转Map
    //以userid为主,重复数据不获取,不会抛出异常Map<Long,UserLoginLog>longDateMap=userLoginLogList.stream().collect(Collectors.toMap(UserLoginLog::getUserId,Function.identity(),(key1,key2)->key1));//业务逻辑if(longDateMap.containsKey(listVo.getUserI......
  • unipp实现map地图轨迹,轨迹长度,标点,连线功能
    效果图 一、pages.json文件中加入{"path":"pages/map/mapd","style":{"navigationBarTitleText":"地图","app-plus":{......
  • 2024年Apache DolphinScheduler RoadMap:引领开源调度系统的未来
    非常欢迎大家来到ApacheDolphinScheduler社区!随着开源技术在全球范围内的快速发展,社区的贡献者“同仁”一直致力于构建一个强大而活跃的开源调度系统社区,为用户提供高效、可靠的任务调度和工作流管理解决方案。在过去的一段时间里,我们取得了一些重要的成就,但我们的愿景远未实......
  • 零拷贝,mmap 和 sendFile
    传统的IOmmapmmap是一种内存映射技术,mmap相比于传统的IO来说,其实就是少了1次CPU拷贝而已,上图。sendFile在Linux中,提供sendFile函数,实现了零拷贝......
  • uniapp nvue页面 map地图全屏设置
    因为nvue页面:100vh以及百分比不可用,所以1,可以获取当前屏幕高度然后赋值<map:latitude="latitude":longitude="longitude":style="'height:'+windowHeight*2+'rpx;'"></map>const{windowWidth,windowHeight,appName}=......
  • SpringBoot:通过实现自定义接口获取实现类的@RequestMapping注解请求路径
    1.自定义接口//什么都不用写,就定义一个空接口publicinterfaceMyMark{}2.Controller接口类实现自定义接口@RestControllerpublicclassDayControllerimplementsMyMark{@RequestMapping("/day1")publicStringget1(){return"day1";}......
  • Java HashMap merge() 方法
    在3020.子集中元素的最大数量【力扣周赛382】用哈希表统计元素个数使用点击查看代码classSolution{publicintmaximumLength(int[]nums){Map<Long,Integer>cnt=newHashMap<>();for(intx:nums){cnt.merge((long)x,1,In......
  • Go - #70: Using mutexes inaccurately with slices and maps
      ......