I came up with a idea of cross checking my knowledge about Vectors and Lists. So, I started off with Qt’s QList and QVector. I created a QList and QVector of 50000 object of another class and then iterated each with ‘for’ loop and ‘foreach’ loop separately. Initially my intentions were to study the time being taken by both kind of loops but I ended up studying Lists and Vectors.
What I found out was ‘foreach’ loop takes more time than plain ‘for’ loop but for this also the time taken for traversing QList is far lesser than for QVector. I though that this might be because of some internal problem with Qt4.2 or with my system’s configuration. Thus, I re-wrote the code using STL List and Vector. I was surprised to see same result as I got for the QList and QVector.
Thus, I fially though of trying out my code in some other system but I got the same result. Still, I can’t understand the reason for this behavior.
You can get my code below :
example.h
#include <QtGui>
#include <QtCore>#define MAX 900000
class Test
{
public:
QString name;
quint32 eid;
QString address;
Test(QString name, quint32 eid, QString address);
Test();
};class Example
{
public:
Example();
};class Example2
{
public:
Example2();
};
example.cpp
#include “example.h”
Example::Example()
{
QList<Test> list;
QVector<Test> vector(50000);
qDebug()<<”starting Example”;QTime currTime = QTime::currentTime();
for(quint32 i = 0; i < 50000; ++i)
{
Test test(QString(“asasasasas”), i, QString(“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”));
list.append(test);
}
qDebug()<<”time taken for appending in list= “<< currTime.msecsTo(QTime::currentTime());currTime = QTime::currentTime();
for(quint32 i = 0; i < 50000; ++i)
{
Test test(QString(“asasasasas”), i, QString(“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”));
vector.append(test);
}
qDebug()<<”time taken for appending in vector = “<< currTime.msecsTo(QTime::currentTime());
Test temp;
currTime = QTime::currentTime();
foreach(temp, list)
{
Test temp2 = temp;
}
qDebug()<<”foreach list = “<< currTime.msecsTo(QTime::currentTime());QList<Test>::iterator it;
currTime = QTime::currentTime();
for(it = list.begin(); it != list.end(); ++it)
{
Test temp2 = *it;
}
qDebug()<<”for list = “<< currTime.msecsTo(QTime::currentTime());quint32 size = list.size();
currTime = QTime::currentTime();
for(quint32 i = 0; i < size; ++i)
{
//Test temp2 = list.at(i);
Test temp2 = list[i];
}
qDebug()<<”for \”i\” list = “<< currTime.msecsTo(QTime::currentTime());currTime = QTime::currentTime();
foreach(temp, vector)
{
Test temp2 = temp;
}
qDebug()<<”foreach vector = “<< currTime.msecsTo(QTime::currentTime());QVector<Test>::iterator iter;
currTime = QTime::currentTime();
for(iter = vector.begin(); iter != vector.end(); ++iter)
{
Test temp2 = *iter;
}
qDebug()<<”for vector = “<< currTime.msecsTo(QTime::currentTime());size = vector.size();
currTime = QTime::currentTime();
for(quint32 i = 0; i < size; ++i)
{
//Test temp2 = vector.at(i);
Test temp2 = vector[i];
}
qDebug()<<”for \”i\” vector = “<< currTime.msecsTo(QTime::currentTime());}
example2.cpp
#include “example.h”
#include <vector.h>
#include <list.h>Test::Test(QString name, quint32 eid, QString address)
{
this->name = name;
this->eid = eid;
this->address = address;
}Test::Test()
{
}Example2::Example2()
{
list<Test> testList;
vector<Test> testVector;
Test testArray[MAX];QTime currTime = QTime::currentTime();
for(int i = 0; i < MAX; ++i)
{
Test temp(QString(“asasasasas”), i, QString(“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”));
testList.push_back(temp);
}
qDebug()<<”time taken for appending in list= “<< currTime.msecsTo(QTime::currentTime());currTime = QTime::currentTime();
for(quint32 i = 0; i < MAX; ++i)
{
Test test(QString(“asasasasas”), i, QString(“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”));
testVector.push_back(test);
}
qDebug()<<”time taken for appending in vector = “<< currTime.msecsTo(QTime::currentTime());currTime = QTime::currentTime();
for(quint32 i = 0; i < MAX; ++i)
{
Test test(QString(“asasasasas”), i, QString(“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”));
testArray[i] = test;
}
qDebug()<<”time taken for appending in Array = “<< currTime.msecsTo(QTime::currentTime());currTime = QTime::currentTime();
list<Test>::iterator itList;
for(itList = testList.begin(); itList != testList.end(); ++itList)
{
Test temp2 = *itList;
}
qDebug()<<”for loop List = “<< currTime.msecsTo(QTime::currentTime());currTime = QTime::currentTime();
vector<Test>::iterator itVector;
for(itVector = testVector.begin(); itVector != testVector.end(); ++itVector)
{
Test temp2 = *itVector;
}
qDebug()<<”for loop Vector = “<< currTime.msecsTo(QTime::currentTime());currTime = QTime::currentTime();
for(int i = 0 ; i < MAX; ++i)
{
Test temp2 = testArray[i];
}
qDebug()<<”for loop Array = “<< currTime.msecsTo(QTime::currentTime());
}
main.cpp
#include <QApplication>
#include <example.h>int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Example ex1;Example2 ex2;
return app.exec();
}
var infolink_pid = 46981;
Posted by Ankit Agarwal 
