Created
January 2, 2014 12:15
-
-
Save rowlo/8218421 to your computer and use it in GitHub Desktop.
Program for demonstrating performance gains by using loop initialization and QList::reserve(int).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include <QDateTime> | |
#include <QList> | |
#include <QObject> | |
#include <QTimer> | |
#include <QDebug> | |
QList<QObject*> buildObjectList() | |
{ | |
qint64 size = 10000000; | |
QList<QObject*> objectList; | |
for (int i = 0; i < size; i++) | |
{ | |
objectList << new QTimer(); | |
} | |
return objectList; | |
} | |
QList<QObject*> buildObjectListWithReserve() | |
{ | |
qint64 size = 10000000; | |
QList<QObject*> objectList; | |
objectList.reserve(size); | |
for (int i = 0; i < size; i++) | |
{ | |
objectList << new QTimer(); | |
} | |
return objectList; | |
} | |
void timeBuildList() | |
{ | |
qint64 tStart = QDateTime::currentMSecsSinceEpoch(); | |
QList<QObject*> list = buildObjectList(); | |
qint64 tEnd = QDateTime::currentMSecsSinceEpoch(); | |
qDebug() << "building list w/o reserve took" << (tEnd - tStart) << "ms"; | |
qDeleteAll(list); | |
} | |
void timeBuildListWithReserve() | |
{ | |
qint64 tStart = QDateTime::currentMSecsSinceEpoch(); | |
QList<QObject*> list = buildObjectListWithReserve(); | |
qint64 tEnd = QDateTime::currentMSecsSinceEpoch(); | |
qDebug() << "building list w/ reserve took" << (tEnd - tStart) << "ms"; | |
qDeleteAll(list); | |
} | |
void timeReverseStackOverflowOneLiner() | |
{ | |
QList<QObject*> list = buildObjectListWithReserve(); | |
qint64 tStart = QDateTime::currentMSecsSinceEpoch(); | |
for(int k = 0; k < (list.size()/2); k++) list.swap(k,list.size()-(1+k)); | |
qint64 tEnd = QDateTime::currentMSecsSinceEpoch(); | |
qDebug() << "reversing list stack overflow took" << (tEnd - tStart) << "ms"; | |
qDeleteAll(list); | |
} | |
void timeReverseStackOverflowOneLinerWithMax() | |
{ | |
QList<QObject*> list = buildObjectListWithReserve(); | |
qint64 tStart = QDateTime::currentMSecsSinceEpoch(); | |
for(int k = 0, max = (list.size()/2); k < max; k++) list.swap(k,list.size()-(1+k)); | |
qint64 tEnd = QDateTime::currentMSecsSinceEpoch(); | |
qDebug() << "reversing list stack overflow with max took" << (tEnd - tStart) << "ms"; | |
qDeleteAll(list); | |
} | |
void timeReverseStackOverflowOneLinerWithMaxAndSize() | |
{ | |
QList<QObject*> list = buildObjectListWithReserve(); | |
qint64 tStart = QDateTime::currentMSecsSinceEpoch(); | |
for(int k=0, s=list.size(), max=(s/2); k<max; k++) list.swap(k,s-(1+k)); | |
qint64 tEnd = QDateTime::currentMSecsSinceEpoch(); | |
qDebug() << "reversing list stack overflow with max and size took" << (tEnd - tStart) << "ms"; | |
qDeleteAll(list); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
timeBuildList(); | |
timeBuildListWithReserve(); | |
timeReverseStackOverflowOneLiner(); | |
timeReverseStackOverflowOneLinerWithMax(); | |
timeReverseStackOverflowOneLinerWithMaxAndSize(); | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment