16,213
社区成员
发帖
与我相关
我的任务
分享
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;
}
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;
}
#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区别不大。