gpt4 book ai didi

c++ - QVector 写入/调整大小性能

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:40 24 4
gpt4 key购买 nike

我试图将一些使用 C 风格整数 vector 的代码重构为 QVector(因为其余代码使用 Qt)。
在我这样做之前,我进行了性能测试以检查此更改会有多糟糕。

我使用这段代码:

#include <QVector>
#include <vector>
#include <cstdio>
#include <ctime>

void test1(int MAX_ELEMENTS, int TIMES) {
int vec[MAX_ELEMENTS];
int nelems = 0;
for (int j=0; j<TIMES; j++) {
nelems = MAX_ELEMENTS;
for (int i=0; i<MAX_ELEMENTS; i++)
vec[i] = 2;
}
printf("Vec[0] = %d\n", vec[0]);
}

void test2(int MAX_ELEMENTS, int TIMES) {
std::vector<int> vec;
vec.reserve(MAX_ELEMENTS);
for (int j=0; j<TIMES; j++) {
vec.clear();
for (int i=0; i<MAX_ELEMENTS; i++)
vec.push_back(2);
}
printf("Vec[0] = %d\n", vec[0]);
}

void test3(int MAX_ELEMENTS, int TIMES) {
QVector<int> vec;
vec.reserve(MAX_ELEMENTS);
for (int j=0; j<TIMES; j++) {
vec.clear();
for (int i=0; i<MAX_ELEMENTS; i++)
vec.push_back(2);
}
printf("Vec[0] = %d\n", vec[0]);
}

void test4(int MAX_ELEMENTS, int TIMES) {
QVector<int> vec;
vec.reserve(MAX_ELEMENTS);
for (int j=0; j<TIMES; j++) {
vec.resize(MAX_ELEMENTS);
for (int i=0; i<MAX_ELEMENTS; i++)
vec[i] = 2;
}
printf("Vec[0] = %d\n", vec[0]);
}

double measureExecutionTime(void (*func)(int, int)) {
const int MAX_ELEMENTS=30000;
const int TIMES=2000000;
clock_t begin, end;
begin = clock();
(*func)(MAX_ELEMENTS, TIMES);
end = clock();
return (double)(end - begin) / CLOCKS_PER_SEC;
}

int main() {
double time_spent;
time_spent = measureExecutionTime(test1);
printf("Test 1 (plain c): %lf\n", time_spent);
time_spent = measureExecutionTime(test2);
printf("Test 2 (std::vector): %lf\n", time_spent);
time_spent = measureExecutionTime(test3);
printf("Test 3 (QVector clear): %lf\n", time_spent);
time_spent = measureExecutionTime(test4);
printf("Test 4 (QVector resize): %lf\n", time_spent);

return 0;
}

结果是:

Vec[0] = 2
Test 1 (plain c): 16.130129
Vec[0] = 2
Test 2 (std::vector): 92.719583
Vec[0] = 2
Test 3 (QVector clear): 109.882463
Vec[0] = 2
Test 4 (QVector resize): 46.261172

为了提高 QVector 的性能,有什么不同的想法吗?这个 vector 每秒从 0 填充到它的新大小几次(它被用在时间表调度软件中)。


Qt版本:5.7.1(+dsfg1,来自Debian测试)

我用来从 Linux shell 编译的命令行:

g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -I. -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -o teste.o teste.cpp

需要说明的是:vector 元素不相等,我只是让它们等于 2。vector 中有效元素的数量随着时间表的事件数量不断变化 - 当给定的事件无法放入时剩余的插槽,算法开始回滚,删除一些最后放置的事件,以便开始另一次调度尝试。

最佳答案

实际上,如MrEricSir在评论部分指出,clear() 操作是 test4 中的真正罪魁祸首。

如果您查看 Qt 文档,您会发现以下内容:

void QVector::clear()

Removes all the elements from the vector.
Note: Until Qt 5.6, this also released the memory used by the vector.From Qt 5.7, the capacity is preserved.

您可能正在使用 Qt 版本 < 5.7,它强制在循环内释放内存。

关于c++ - QVector<int> 写入/调整大小性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com