PDA

View Full Version : QList < QHash < int, QString> > ... clear() is slow



JoZCaVaLLo
14th March 2011, 15:49
I'm translating an old application into qt...
I started doing a QList < QString > as it was the original algorithm, but this was meaning looking into the string everytime I needed a value.

I now translated the <QString> into a QHash <int, QString>, witch applies perfectly to my purpose...

:mad: BUT NOW... :mad:

When I do QList::clear() ... this turn into an extremely slow operation, 3x more slow then keeping my old algorithm.

What can I do to improve performances of clear() ??? Does it dependes on QList or on QHash??? :confused:

Do I have to turn into a QSqlTableModel??? But this would mean to break all old algorithms and the Boss wouldn't be happy :(

wysota
14th March 2011, 19:12
QList::clear()? What QList? You said you had a QHash...

conner686
14th March 2011, 19:14
You replaced a list of strings with a list of hashes? Or just a single hash, with the old index as the key? How did your non-qt algorithm find strings without looking at them? If it was just a lookup by index, why can't you just do that with QList<QString>? what does QSqlTableModel have to do with anything? In short, you're missing a lot of information, and I'll bet dollars to donuts clear() isnt your problem.

ChrisW67
15th March 2011, 00:25
Mmmmm doughnuts :)

I think the final structure was QList< QHash<int, QString> > and that the clear() method is being called on the outer QList. This would result in the destructor of each QHash being called, which would in turn call the destructor of each QString. If there is a list of hundreds of hashes of hundreds of keys then the numbers get large quickly but still should not be a massive imposition unless you do it repeatedly.

JoZCaVaLLo
15th March 2011, 08:22
Thank you all for your answers.

The main point is that now I represent a table with a QList < QHash <int, QString> >. This has improved performances until I call QList::clear(), this is so slow that at the end, the overall performances are much better with the old program (about 3x better!!!!!!!!!!) :eek::eek::eek:

The QHash can have a length of max 30. But the QList is about 200'000 elements. The clear() of such a QList takes about 20 minutes on a Intel 2.66 GHz 4 Gbytes Ram. :eek:
The same QList, but with QStrings instead of QHashes, takes ~5 minutes.

My question is: Do I have better time to
1) reimplement the clear() of QHash and QList?
2) keep the QList <QString> ?
3) look for another container?

nightghost
15th March 2011, 08:48
We can not give an correct answer without seeing the algorithm, but even this could only a guess. (Maybe you introduced a bug that has nothing to do with the new algorithm)

You will get the best answer this way:

1.) Run the program with a profiler and check were the bottleneck is (you will be suprised ;))
2.) Optimize the bottleneck.
3.) Repeat 1.) + 2.) until the performance is ok.

wysota
15th March 2011, 10:13
Run the attached project and post your results. Mine are:

********* Start testing of HashListBenchmark *********
Config: Using QTest library 4.7.0, Qt 4.7.0
PASS : HashListBenchmark::initTestCase()
RESULT : HashListBenchmark::clearHash():
996 msecs per iteration (total: 996, iterations: 1)
PASS : HashListBenchmark::clearHash()
PASS : HashListBenchmark::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of HashListBenchmark *********

The test machine is:
2.6.35-28-generic #49-Ubuntu SMP Tue Mar 1 14:39:03 UTC 2011 x86_64
vendor_id : AuthenticAMD
model name : AMD Phenom(tm) II X6 1055T Processor
cpu MHz : 800.000 (2800 max)
MemTotal: 4053732 kB

JoZCaVaLLo
15th March 2011, 11:04
Thanks Wysota
The problem is running the app in debugging from Visual Studio:

This is the result running your test without debugging


********* Start testing of HashListBenchmark *********
Config: Using QTest library 4.6.3, Qt 4.6.3
PASS : HashListBenchmark::initTestCase()
RESULT : HashListBenchmark::clearHash():
1,343 msec per iteration (total: 1343, iterations: 1)
PASS : HashListBenchmark::clearHash()
PASS : HashListBenchmark::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of HashListBenchmark *********

with debugging is still running after 30 minutes... :p

wysota
15th March 2011, 11:07
Well, I'm not surprised. Debugging is slow.