Skip to content

Instantly share code, notes, and snippets.

@agateau
Created March 18, 2013 13:44
Show Gist options
  • Save agateau/5187229 to your computer and use it in GitHub Desktop.
Save agateau/5187229 to your computer and use it in GitHub Desktop.
Benchmarks how QList::reserve() affects QList performance
// Build with: g++ benchqlist.cpp -o benchqlist -I/usr/include/qt4 -Wall -lQtCore -lstdc++ -O2
#include <QtCore/QList>
#include <QtCore/QElapsedTimer>
#include <QtCore/QDebug>
#if 1
#define ELEMENT int
#define ELEMENT_VALUE 12
#else
struct MyStruct {
int x, y;
QString text;
char data[100];
};
#define ELEMENT MyStruct
#define ELEMENT_VALUE MyStruct()
#endif
int main(int argc, char *argv[])
{
if (argc < 2) {
qWarning() << "Usage: benchqlist [-r] <number-of-iterations>";
return 1;
}
bool reserve = false;
QString arg = QString::fromUtf8(argv[1]);
if (arg == "-r") {
reserve = true;
Q_ASSERT(argc == 3);
arg = QString::fromUtf8(argv[2]);
}
bool ok;
int count = arg.toInt(&ok);
Q_ASSERT(ok);
qWarning() << "Testing with" << count << "elements," << (reserve ? "using reserve" : "not using reserve");
QList<ELEMENT> list;
if (reserve) {
list.reserve(count);
}
QElapsedTimer timer;
timer.start();
for (int x = count - 1; x >= 0; --x) {
list.append(ELEMENT_VALUE);
}
qWarning() << timer.elapsed() / 1000.;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment