使用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