QHash - Lost performance on Ubuntu
-
Hi everybody,
I have a code that run in a portable application (Ubuntu 12 and windows 7).
This application was compiled using VC++ 2010 and Qt 4.7 for Windows and for Ubuntu using g++ 4.4 and Qt 4.8.
Optimization flag setted as O2 (on both environments).I have a QHash data structure and two algorithms to looking up its.
The first algorithms looking up by keys and the other by values. The choice between theses two algorithms is done by a statistic function.
In the tested code I need to perform a looking up by values and the result on Ubuntu is 5 times slow than on Windows ones.Windows 7: 102.19 ms
Ubuntu 12: 505.71 msMy structure is something like this and the performance analysis was done in "perform()" method:
@
class InnerObject
{
public:
QString key;
double value;InnerObject(); ~InnerObject();
};
class MyObject
{
public:
MyObject();
~MyObject();private:
QHash<QString, InnerObject*> inner;
int id;
};class Collection
{
public:
Collection();
~Collection();void perform(){ // (...) for(MyObject *myObj, collection.values()) { if (myObj) { // do something... } }
}
private:
QHash<int, MyObject *> collection;};
@Anyone knows what is happening??
Thanks!
-
There are two thing I would try.
- Refactor the code to using <code>foreach</code> or an iterator (not <code>.values()</code>)
@
foreach(MyObject* myObj, collection)
...
for (QHash<int, MyObject *>::Iterator myObj = collection.begin();
myObj != collection.end();
++myObj)
...
@- Move away from the ancient GCC 4.4 to 4.6 or better 4.7.
- Refactor the code to using <code>foreach</code> or an iterator (not <code>.values()</code>)
-
To get even more performance, use Qt5 built with C++11 support (GCC 4.7/ 4.8 or clang). Hints from Lukas are even better, though :)
-
Lukas,
I changed 'foreach' by iterator and ran a little test on windows. The performance improves in ~33%.
Sierdzio,
Changes my 3rd party libraries isn't a possibility for now.
I'm running the real test now... after analysis I will post here...
Thanks!! -
How about using a datastructure that is actually optimized for iteration over all elements?
Fiddling with making QHash fast for this use-case is bound to be micro-optimizations only.
-
[quote author="Tobias Hunger" date="1363983457"]How about using a datastructure that is actually optimized for iteration over all elements?[/quote]
Tobias, I have two algorithms that may be used and one of them is chose in runtime (by statistical function). Hash was chose because its complexity and there are moments that I get the object by key too.
There is always a trade-off that must be considered as everything in computing.[quote author="Tobias Hunger" date="1363983457"]Fiddling with making QHash fast for this use-case is bound to be micro-optimizations only.[/quote]
I don't discover yet what is the problem between platforms but the same code is faster on Windows and very slow on Ubuntu.On my tests, I could see that the problem isn't in the hash per si. This should be in the hash implementation, Ubuntu's scheduler, both or another thing.
[]'s