The thing I am trying to achieve here is to make a statement of the performance of Qt object creation and destruction. If you remember, I asked a couple of weeks ago what would be the disadvantage of making every object (model or view or whatever) a QObject. Since a QObject does something useful, there must be clearly ome overhead involved.
I must mention that I am a fan of generating code. And, in the meantime, I worked on a prototype which generates a QObject for each and every class in my UML tool. In addition to that, I also generate a QObject for each and every property of that UML class. Reason is, I later want to be able to connect to the attribute of the concrete instance of that class and receive events via signals/slots. However, my first test showed me that the performance was not good. And especially memory consumption was a killer. Creation of an object (QObject) with 20 such property objects (also QObjects) took ~20µs. So I decided to step back and do the old school approach and map the UML properties onto C++ plain old data members.
Anyhow, once I changed the code generator I switched over to the new Qt 4.6 from the previous Qt-4.6-tp1 and recognized a significant drop in performance of the destruction process. That is why I stripped down my code to a presentable minimum without using too many Qt features at once, like QObjectList or something. Plain QObjects should be constructable and destructable, right? It doesn't matter, where I keep them. I just wanted to make sure that the compiler doesn't optimize them out.
I'm still not through to this. But I have recognized so far that as soon as I do not wire the objects together via parent-child all is fine. If I connect them, performance drop to inacceptible level.
int main(int argc, char *argv[])
{
const int COUNT = 30000;
Timer clock;
clock.start();
for(int i=0; i<COUNT; i++) {
}
qDebug() << "creation of" << COUNT << "objects: " << clock.getElapsedTimeInMilliSec() << "ms = "
<< clock.getElapsedTimeInMicroSec() / COUNT << "us per object";
clock.start();
for(int i=0; i<COUNT; i++) {
qls[i]->setParent(0);
delete qls[i];
}
delete[] qls;
delete parent;
qDebug() << "destruction of" << COUNT << "objects: " << clock.getElapsedTimeInMilliSec() << "ms = "
<< clock.getElapsedTimeInMicroSec() / COUNT << "us per object";
return 0;
}
int main(int argc, char *argv[])
{
const int COUNT = 30000;
QCoreApplication a(argc, argv);
Timer clock;
clock.start();
QObject *parent = new QObject;
QObject **qls = new QObject*[COUNT];
for(int i=0; i<COUNT; i++) {
qls[i] = new QObject(parent);
}
qDebug() << "creation of" << COUNT << "objects: " << clock.getElapsedTimeInMilliSec() << "ms = "
<< clock.getElapsedTimeInMicroSec() / COUNT << "us per object";
clock.start();
for(int i=0; i<COUNT; i++) {
qls[i]->setParent(0);
delete qls[i];
}
delete[] qls;
delete parent;
qDebug() << "destruction of" << COUNT << "objects: " << clock.getElapsedTimeInMilliSec() << "ms = "
<< clock.getElapsedTimeInMicroSec() / COUNT << "us per object";
return 0;
}
To copy to clipboard, switch view to plain text mode
compiled and run against "Qt 4.6 technology preview" takes
creation of 30000 objects: 14.759 ms = 0.491933 us per object
destruction of 30000 objects: 16.539 ms = 0.5513 us per object
creation of 30000 objects: 14.759 ms = 0.491933 us per object
destruction of 30000 objects: 16.539 ms = 0.5513 us per object
To copy to clipboard, switch view to plain text mode
whereas run under Qt-4.6 GA takes
creation of 30000 objects: 18.028 ms = 0.6009 us per object
destruction of 30000 objects: 2127.7 ms = 70.9234 us per object
creation of 30000 objects: 18.028 ms = 0.6009 us per object
destruction of 30000 objects: 2127.7 ms = 70.9234 us per object
To copy to clipboard, switch view to plain text mode
But: if I don't connect the objects via parent-child, like here
for(int i=0; i<COUNT; i++) {
}
for(int i=0; i<COUNT; i++) {
delete qls[i];
}
QObject *parent = new QObject;
QObject **qls = new QObject*[COUNT];
for(int i=0; i<COUNT; i++) {
qls[i] = new QObject;
}
for(int i=0; i<COUNT; i++) {
delete qls[i];
}
To copy to clipboard, switch view to plain text mode
then I get excellent performance, even in the GA version:
creation of 30000 objects: 9.654 ms = 0.321767 us per object
destruction of 30000 objects: 14.067 ms = 0.468867 us per object
creation of 30000 objects: 9.654 ms = 0.321767 us per object
destruction of 30000 objects: 14.067 ms = 0.468867 us per object
To copy to clipboard, switch view to plain text mode
May be this is a bug which has no consequence in a UI application due to low amounts of objects and low response time expectancy during human interactions. I don't even know who to ask or where to go at Qt's website to open a ticket for that. ???
Bookmarks