Qt與STL容器效率比較
阿新 • • 發佈:2021-07-25
Qt與STL容器效率比較
測試平臺:windows10 Intel 7700HQ
編譯器:MSVC2017*64 -O2
vector
插入500000000個int值並刪除,測試執行時間,程式碼如下:
#include "mainwindow.h" #include <iostream> #include <QApplication> #include <vector> #include <QVector> #include <windows.h> std::vector<int> stl; QVector<int> Qtv; int main(int argc, char *argv[]) { LARGE_INTEGER beg,ed,freq; std::cout<<"begin test!"<<std::endl; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&beg); for(int i = 1; i<=500000000; i++) { stl.push_back(i); } stl.clear(); QueryPerformanceCounter(&ed); double duration = (double)(ed.QuadPart-beg.QuadPart)/(double)freq.QuadPart; std::cout<<"std:"<<duration<<std::endl; QueryPerformanceCounter(&beg); for(int i = 1; i<=500000000; i++) { Qtv.push_back(i); } Qtv.clear(); QueryPerformanceCounter(&ed); duration = (double)(ed.QuadPart-beg.QuadPart)/(double)freq.QuadPart; std::cout<<"Qt:"<<duration<<std::endl; return 0; }
結果:單位:s
專案 | 測試1 | 測試2 | 測試3 |
---|---|---|---|
stl | 4.49656 | 4.26468 | 4.30204 |
qt | 4.69023 | 4.74365 | 4.69194 |
如果是1000000000的資料量的話STL需要7.5s,而Qt會直接卡死。綜上來看,stl的vector效率高於QVector。
set
插入50000000組資料,比較時間,程式碼:
#include "mainwindow.h" #include <iostream> #include <QApplication> #include <set> #include <QSet> #include <cstdlib> #include <windows.h> std::set<int> stl; QSet<int> Qtset; int main(int argc, char *argv[]) { LARGE_INTEGER beg,ed,freq; std::cout<<"begin test!"<<std::endl; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&beg); for(int i = 1; i<=50000000; i++) { stl.insert(i); } stl.clear(); QueryPerformanceCounter(&ed); double duration = (double)(ed.QuadPart-beg.QuadPart)/(double)freq.QuadPart; std::cout<<"std:"<<duration<<std::endl; QueryPerformanceCounter(&beg); for(int i = 1; i<=50000000; i++) { Qtset.insert(i); } Qtset.clear(); QueryPerformanceCounter(&ed); duration = (double)(ed.QuadPart-beg.QuadPart)/(double)freq.QuadPart; std::cout<<"Qt:"<<duration<<std::endl; return 0; }
專案 | 測試1 | 測試2 | 測試3 |
---|---|---|---|
stl | 13.8065 | 12.876 | 12.6713 |
qt | 6.56534 | 6.13544 | 6.20306 |
可見,QSet效率遠高於STL.