Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Which is faster: QMetaObject::invokeMethod or connect & emit signal
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Which is faster: QMetaObject::invokeMethod or connect & emit signal

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 8.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marcus.fr
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      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!

      1 Reply Last reply
      0
      • M Offline
        M Offline
        marcus.fr
        wrote on last edited by
        #3

        [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?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          Passing strings should be quite lightweight, as they are implicitly shared.

          Are you already running into performance problems?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            marcus.fr
            wrote on last edited by
            #5

            [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

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              Honestly, I don't know, maybe because the article focus is on design, not performance.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved