[Qt-interest] performance issue
Daniel Bowen
qtmailinglist1 at bowensite.com
Fri Jan 8 09:02:37 CET 2010
Thanks for the tips.
So, what would be most efficient for most platforms but still robust for,
say, reading all the contents of a file, and writing to another QIODevice?
I see that in 4.6.0, QFile::copy implements the fallback path with atEnd:
char block[4096];
qint64 totalRead = 0;
while(!atEnd()) {
qint64 in = read(block, sizeof(block));
if (in <= 0)
break;
totalRead += in;
if(in != out.write(block, in)) {
d->setError(QFile::CopyError, tr("Failure to
write block"));
error = true;
break;
}
}
There are other things like using QXmlStreamReader, where the normal thing
is to use atEnd in a loop. Doesn't that end up triggering QFile::atEnd?
Could atEnd be made more efficient for more cases?
Thanks,
-Daniel
-----Original Message-----
From: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] On Behalf Of João Abecasis
Sent: Thursday, January 07, 2010 9:22 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] performance issue
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
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list