[Qt-interest] performance issue

João Abecasis joao at trolltech.com
Thu Jan 7 17:22:11 CET 2010


For those interested in performance,

I summarize a few suggestions for the scenario:

Paul Floyd wrote:
> I've just been doing a few tests on a small app that mostly does regex parsing
> of a text file (56Mbyte). Times were as follows

- Avoid QFile::atEnd, especially while reading from a file in a loop 
(see snippet further down for an alternative). QFile::atEnd stats the 
filesystem and this is usually not necessary.

- There's a double-buffering issue in Qt 4.3.0 to 4.6.0 (possibly 4.6.1 
as well) for files opened in Buffered mode. A fix has been committed to 
the 4.6 branch (see separate mail in this thread).

- Compilation of regular expressions is expensive, so try to reuse 
QRegExp instances. Internally Qt keeps a cache of compiled regexes, but 
these are only kept while at least one instance of QRegExp is alive.

- QString <=> QByteArray conversions are expensive. When processing text 
make sure you don't perform the same conversions repeatedly. 
QIODevice::readLine() returns a QByteArray; if you don't care about the 
byte contents at all, convert it as soon as possible to a QString and 
reuse that:

     QString line;
     while (line = device.readLine(), !line.isEmpty()) {
         // do stuff...
     }

- For heavier QString processing, checkout a couple of optimizations 
introduced in Qt 4.6:

 
http://doc.trolltech.com/4.6/qstring.html#more-efficient-string-construction

Admittedly, the last suggestion will be more important if concatenating 
QStrings is your bottleneck. And to find those, you need to profile your 
code...

Anyway, I hope this is helpful.

Cheers,


João



More information about the Qt-interest-old mailing list