测试环境:windows11
测试IDE:qt creator 13.0.1 Qt:6.2.3
测试编译器:MSVC 2019 64bit
代码
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
#include <QDebug>
#include <random>
using namespace std;
class Test {
public:
int m_key;
QString m_v;
};
#define NUM 10000000
#define START 5000
#define END 60000
static random_device rd;
static mt19937 gen(rd());
static uniform_int_distribution<> random(1, NUM - 2);
static QList<QSharedPointer<Test>> list1;
static std::vector<QSharedPointer<Test>> vector1;
void init() {
for (int i = 0; i < NUM; ++i) {
auto v = QSharedPointer<Test>(new Test());
v->m_key = i;
v->m_v = QString::number(i);
list1.push_back(v);
vector1.push_back(v);
}
}
void testInsertHead() {
cout << "insert head" << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
list1.push_front(v);
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
vector1.insert(vector1.begin(), v);
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testInsertTail() {
cout << "insert tail" << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
list1.push_back(v);
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
vector1.push_back(v);
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testInsertAnyPosi() {
int posi = random(gen);
cout << "insert" << posi << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
list1.insert(posi, v);
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
vector1.insert(vector1.cbegin() + posi, v);
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testDeleteHead() {
cout << "delete head" << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
list1.pop_front();
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
vector1.erase(vector1.begin());
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testDeleteTail() {
cout << "delete tail" << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
list1.pop_back();
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
vector1.pop_back();
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testDeleteAnyPosi() {
int posi = random(gen);
cout << "delete" << posi << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
list1.remove(posi);
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = QSharedPointer<Test>(new Test());
v->m_key = 0;
v->m_v = QString::number(0);
vector1.erase(vector1.cbegin() + posi);
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testCopyLoop() {
cout << "copy loop" << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
QList<QSharedPointer<Test>> list2;
list2.resize(END - START + 1);
for (int i = START; i < END; ++i) {
list2.push_back(list1.at(i));
}
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
std::vector<QSharedPointer<Test>> vector2;
vector2.resize(END - START + 1);
std::copy(vector1.begin() + START, vector1.begin() + END, vector2.begin());
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testCopyIt() {
cout << "copy iterator constructor" << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
QList<QSharedPointer<Test>> list2(list1.begin() + START, list1.begin() + END);
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
std::vector<QSharedPointer<Test>> vector2;
vector2.resize(END - START + 1);
std::copy(vector1.begin() + START, vector1.begin() + END, vector2.begin());
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testQueryHead() {
cout << "query head" << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = list1[0];
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = vector1[0];
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testQueryTail() {
cout << "query tail" << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = list1[list1.size() - 1];
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = vector1[vector1.size() - 1];
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
void testQueryAnyPosi() {
int posi = random(gen);
cout << "query" << posi << endl;
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = list1[posi];
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
{
auto m1 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
auto v = vector1[posi];
auto m2 = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
cout << m2 - m1 << endl;
}
}
int main(int argc, char* argv[]) {
init();
testInsertHead();
testInsertTail();
testInsertAnyPosi();
testDeleteHead();
testDeleteTail();
testDeleteAnyPosi();
testQueryHead();
testQueryTail();
testQueryAnyPosi();
testCopyLoop();
testCopyIt();
return 0;
}
运行结果
结论
QList性能能满足大部分场景,vector在删除头时性能逊于QList
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。