C++11 for 新语法,以及QVector 和 vector性能差异

王桑的一天 2019-10-11 10:04:35

int main(int argc, char *argv[])
{
clock_t start, end;
srand(32);

// create array
QVector<double> lst = QVector<double>(65535);
for (size_t i = 0; i < lst.size(); ++i)
{
lst[i] = rand() % 400 + 1;
}

double max_v = 0, min_v = 0;
start = clock();
double temp;
for (int j = 0; j < 3000; ++j)
{
for (const double &item : lst)
{
temp = item * 0.0098;
if (max_v < temp)
max_v = temp;
if (min_v > temp)
min_v = temp;
}
}
end = clock();
cout << "Time cost: " << double(end - start)/CLOCKS_PER_SEC << endl;

max_v = min_v = 0;
start = clock();
for (int j = 0; j < 3000; ++j)
{
for (size_t i = 0; i < lst.size(); ++i)
{
temp = lst[i] * 0.0098;
if (max_v < temp)
max_v = temp;
if (min_v > temp)
min_v = temp;
}
}

end = clock();
cout << "Time cost: " << double(end - start)/CLOCKS_PER_SEC << endl;
return 0;
}

Foreach time cost: 0.687
For time cost: 8.697



int main(int argc, char *argv[])
{
clock_t start, end;
srand(32);

// create array
vector<double> lst = vector<double>(65535);
for (size_t i = 0; i < lst.size(); ++i)
{
lst[i] = rand() % 400 + 1;
}

double max_v = 0, min_v = 0;
start = clock();
double temp;
for (int j = 0; j < 3000; ++j)
{
for (const double &item : lst)
{
temp = item * 0.0098;
if (max_v < temp)
max_v = temp;
if (min_v > temp)
min_v = temp;
}
}
end = clock();
cout << "Foreach time cost: " << double(end - start)/CLOCKS_PER_SEC << endl;

max_v = min_v = 0;
start = clock();
for (int j = 0; j < 3000; ++j)
{
for (size_t i = 0; i < lst.size(); ++i)
{
temp = lst[i] * 0.0098;
if (max_v < temp)
max_v = temp;
if (min_v > temp)
min_v = temp;
}
}

end = clock();
cout << "For time cost: " << double(end - start)/CLOCKS_PER_SEC << endl;
return 0;
}


Foreach time cost: 2.278
For time cost: 1.294


问题:
1. 使用QVector ,使用 for 新语法遍历数组快了10倍,只要0.687s,为啥 ?
2. 使用vector,使用 for 新语法遍历数组反而慢,但差距小了,why?
QVector 和 vector 如何选择,可以根据这个结果作参考吗?
...全文
920 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
donwmufromdying 2019-10-12
  • 打赏
  • 举报
回复
引用 4 楼 dext 的回复:
首先我觉得用Debug模式比较没有什么意义,因为Qt STL很多模板库 是针对 编译器的优化程度而设计的。有的时候 release 模式快,反而debug更慢。 用O2编译 两个都是瞬时完成,没有比较意义。
还真是这样。
王桑的一天 2019-10-11
  • 打赏
  • 举报
回复
mingw编译器,Win7x64
dext 2019-10-11
  • 打赏
  • 举报
回复
首先我觉得用Debug模式比较没有什么意义,因为Qt STL很多模板库 是针对 编译器的优化程度而设计的。有的时候 release 模式快,反而debug更慢。

用O2编译 两个都是瞬时完成,没有比较意义。
王桑的一天 2019-10-11
  • 打赏
  • 举报
回复
引用 2 楼 donwmufromdying 的回复:
结果是:
QVector, traditional mode, cost time: 12441 milliseconds
QVector, foreach mode, cost time: 3317 milliseconds
std::vector, traditional mode,cost time: 27114 milliseconds
std::vector, foreach mode, cost time: 2589 milliseconds

所以,感觉foreach模式确实快很多。数量级上的。QVector和std::vector区别不大。


我这边测试好像不是哦。
QVector 取值改成at(i) 更快些。
我在家里电脑结果是:
Foreach time cost: 0.464
For time cost: 2.044

然后用vector,却相反,使用 [i]取值更快:
但结果是foreach 更慢:
Foreach time cost: 1.74
For time cost: 0.827

你的代码,vector 改用 [i]取值后,也是foreach 慢了
QVector, traditional mode, cost time: 2911 milliseconds
QVector, foreach mode, cost time: 673 milliseconds
std::vector, traditional mode,cost time: 1429 milliseconds
std::vector, foreach mode, cost time: 2873 milliseconds

总结来说就是
QVector 用 foreach 更快,4~5倍
vector 用传统 for 更快,2 倍
donwmufromdying 2019-10-11
  • 打赏
  • 举报
回复
#include <QCoreApplication>
#include <QVector>
#include <QElapsedTimer>
#include <QtDebug>
#include <vector>

void doTest( double data) {
    double tmp = data*0.0098;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QVector<double> array;
    for(int i=0;i<65536; ++i) {
        array.push_back( rand() % 400 + 12.12 );
    }

    QElapsedTimer timer;
    timer.start();

    for( int j=0;j<4096;++j)
    {
        for(int k=0;k<array.size();++k) {
            doTest(array.at(k));
        }
    }
    qDebug()<<"QVector, traditional mode, cost time:"<<timer.elapsed()<<" milliseconds";

    timer.restart();
    for( int j=0;j<4096;++j)
    {
        for( double& item : array) {
            doTest( item );
        }
    }
    qDebug()<<"QVector, foreach mode, cost time:"<<timer.elapsed()<<" milliseconds";


    std::vector<double> array2;
    for(int i=0;i<65536; ++i) {
        array2.push_back( rand() % 400 + 12.12 );
    }

    timer.restart();

    for( int j=0;j<4096;++j)
    {
        for(int k=0;k<array2.size();++k) {
            doTest(array2.at(k));
        }
    }
    qDebug()<<"std::vector, traditional mode,cost time:"<<timer.elapsed()<<" milliseconds";

    timer.restart();
    for( int j=0;j<4096;++j)
    {
        for( double& item : array2) {
            doTest( item );
        }
    }
    qDebug()<<"std::vector, foreach mode, cost time:"<<timer.elapsed()<<" milliseconds";



    return a.exec();
}
结果是: QVector, traditional mode, cost time: 12441 milliseconds QVector, foreach mode, cost time: 3317 milliseconds std::vector, traditional mode,cost time: 27114 milliseconds std::vector, foreach mode, cost time: 2589 milliseconds 所以,感觉foreach模式确实快很多。数量级上的。QVector和std::vector区别不大。

16,213

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧