⬆️ ⬇️

Small QCoreApplication speed test

For reliability and security, it often makes sense to divide the functionality of an application into many small executable files (daemons) instead of supporting a large and monolithic application.



At one time, Qt4 was divided into several modules, which in particular made it possible to create applications that do not require a graphical interface. Thanks to a strong focus on embedded systems architecture and several sensible architecture solutions, Qt5 has taken this approach to a new level.



Let's look at a simple function:

QCoreApplication app(argc, argv); QTimer::singleShot(3000, &app, SLOT(quit())); return app.exec(); 


This is a Qt application that does not have a graphical interface. It runs for 3 seconds and then ends.



On my Kubuntu 12.04 with a standard kernel and Qt 4.8.1, Valgrind shows a peak heap usage of 102 kb, while Callgrind reports that 1.9 (*) million processor instructions have been executed.

')

Let's look at the numbers obtained on today's Qt 5 build: Valgrind reports 4.9 KB of memory allocation on the heap, and Callgrind reports about 114,000 processor instructions.



This means that Qt 5 uses about 20 times less memory and about 16 times less processor instructions in the QCoreApplication constructor and to execute an Event-loop.



There are several reasons for this. In particular, Qt 5 assumes that all strings are unicode-encoded by default, so the text encoding converter is initialized only when the first non-Unicode string appears. Although plug-in loading performance has significantly improved in Qt 5, they simply cannot load even faster.



Other improvements also affected the speed, for example, the introduction of C ++ 11 support in Qt5 allowed to create Unicode strings that do not require memory allocation on the heap.



In general, try writing your demons to Qt in a fun way and if you have any ideas on how to optimize the code, welcome in Qt Codereview .



(*) Warning: The number of instructions does not show how fast the code is, but only the number of instructions actually processed by the processor. Please note that in all cases I only measured the performance of the main () function, ignoring the loading costs of the shared libraries, so the application can be further optimized using pre-linking or a direct fork from an already running process.



From the translator: if such small translations take root and I, as a translator, are not a complete asshole in your eyes, then I will try to continue translating notes from the Qt Labs blog. Thank you =)

Source: https://habr.com/ru/post/149580/



All Articles