[SOLVED] Which is faster: QMetaObject::invokeMethod or connect & emit signal
-
Hi
I have an object that is created and destructed many times in a small interval. It lives in the secondary thread and it sends data to the main thread using QMetaObject::invokeMethod(). I use all 10 available parameters of QMetaObject::invokeMethod() and it seems that on each call it is parsing the parameters.
Will it be faster if I use connect() and emit Signal inside the object each time it is created instead of QMetaObject::invokeMethod()?
Thanks
-
QMetaMethod::invoke(), emit, QMetaObject::invokeMethod() (in this order, depending on the actual situation, with the usual reserve), but the actual question is, will the difference matter resp. is this difference your bottleneck.
"An object that is created and destructed many times in a small interval", "sends data to the main thread" and "use all 10 available parameters of QMetaObject::invokeMethod()" raises all red performance flags on me.
Creation and destruction (of course depending on the object) is one of the most costliest operations. Do you really need use a new object on each iteration or is it possible to re-use an already existing operation?
Passing data using arguments is another (again, depending on the object) costly operation. If you need to pass a large amount of data shared memory in combination with a change signal is most probably a better solution.
If you absolutely need to pass the data using arguments try to reduce the number of arguments as much as possible. Try to pack them in a struct and preferably pass references instead of values; in addition, this will shorten the method signature and the argument list.
And as with every performance optimization: profile first!
-
[quote author="Lukas Geyer" date="1355759698"]QMetaMethod::invoke(), emit, QMetaObject::invokeMethod() (in this order, depending on the actual situation, with the usual reserve), but the actual question is, will the difference matter resp. is this difference your bottleneck.[/quote]
I overlooked the existence of QMetaMethod::invoke(). Thanks, It's a good one![quote author="Lukas Geyer" date="1355759698"]"An object that is created and destructed many times in a small interval", "sends data to the main thread" and "use all 10 available parameters of QMetaObject::invokeMethod()" raises all red performance flags on me.
Creation and destruction (of course depending on the object) is one of the most costliest operations. Do you really need use a new object on each iteration or is it possible to re-use an already existing operation?[/quote]
I put as much code as I could in reusable objects. However the object in question wraps QTcpSocket and I didn't had good experience with reusing QTcpSocket object for connection to different hosts.
[quote author="Lukas Geyer" date="1355759698"]
Passing data using arguments is another (again, depending on the object) costly operation. If you need to pass a large amount of data shared memory in combination with a change signal is most probably a better solution.If you absolutely need to pass the data using arguments try to reduce the number of arguments as much as possible. Try to pack them in a struct and preferably pass references instead of values; in addition, this will shorten the method signature and the argument list.
And as with every performance optimization: profile first![/quote]
Thanks for the suggestions. I pass pointers to large objects that live in the main thread. I do however pass some QStrings with a lenght of max 1000 chars, but the QString content is initially created in a secondary thread so I guess the optimal solution is to pass it's data in the parameter, or maybe I'm wrong?
-
[quote author="Lukas Geyer" date="1355829966"]Passing strings should be quite lightweight, as they are implicitly shared.
Are you already running into performance problems?[/quote]
Not quite. There are some performance issues when I use Application Verifier with Low resource simulation enabled (Windows). I know that profiling should be done before micro-optimizing but in this case I really want to do it right from the begging instead of recoding it later. There is not much difference in the effort in coding with QMetaMethod::invoke(), emit or QMetaObject::invokeMethod().
I wonder why QMetaMethod::invoke() is not in the focus in the docs, for instance in this wiki page it is not mentioned at all: "http://qt-project.org/wiki/Threads_Events_QObjects":http://qt-project.org/wiki/Threads_Events_QObjects