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. Why is timer so slow?
Forum Updated to NodeBB v4.3 + New Features

Why is timer so slow?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 633 Views 2 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 19 May 2021, 06:47 last edited by SPlatten
    #1

    I've created an instance of QTimer to perform a regular function.

    mptmrUpload = new QTimer(this);
    QObject::connect(mptmrUpload, &QTimer::timeout, [this]() {
        if ( muint16Chunk < muint16Total ) {
           mpui->pgrbrStatus->setValue(++muint16Chunk);
           mpui->pgrbrStatus->update();
        }
    });
    mptmrUpload->start(ImportRDF::mscuint16UploadInterval);
    

    I added the call to update in an effort to see if it improved performance, it didn't make any difference. Definition of mscuint16UploadInterval:

    const quint16 ImportRDF::mscuint16UploadInterval = 10;
    

    pgrbrStatus is a progress bar in a dialog, the progress bar is incrementing approximately once every 2 seconds...whats going on?

    [Edit] Just noticed there isn't an overloaded version of start for an integer like there is for setInterval so I've changed my definition of mscuint16UploadInterval to:

    const std::chrono::milliseconds ImportRDF::mscmsUploadInterval(10);
    

    Kind Regards,
    Sy

    J 1 Reply Last reply 19 May 2021, 06:55
    0
    • S SPlatten
      19 May 2021, 06:47

      I've created an instance of QTimer to perform a regular function.

      mptmrUpload = new QTimer(this);
      QObject::connect(mptmrUpload, &QTimer::timeout, [this]() {
          if ( muint16Chunk < muint16Total ) {
             mpui->pgrbrStatus->setValue(++muint16Chunk);
             mpui->pgrbrStatus->update();
          }
      });
      mptmrUpload->start(ImportRDF::mscuint16UploadInterval);
      

      I added the call to update in an effort to see if it improved performance, it didn't make any difference. Definition of mscuint16UploadInterval:

      const quint16 ImportRDF::mscuint16UploadInterval = 10;
      

      pgrbrStatus is a progress bar in a dialog, the progress bar is incrementing approximately once every 2 seconds...whats going on?

      [Edit] Just noticed there isn't an overloaded version of start for an integer like there is for setInterval so I've changed my definition of mscuint16UploadInterval to:

      const std::chrono::milliseconds ImportRDF::mscmsUploadInterval(10);
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 19 May 2021, 06:55 last edited by
      #2

      @SPlatten said in Why is timer so slow?:

      whats going on?

      You're probably blocking the event loop somewhere. Impossible to say without more information.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 19 May 2021, 07:02
      1
      • J jsulm
        19 May 2021, 06:55

        @SPlatten said in Why is timer so slow?:

        whats going on?

        You're probably blocking the event loop somewhere. Impossible to say without more information.

        S Offline
        S Offline
        SPlatten
        wrote on 19 May 2021, 07:02 last edited by
        #3

        @jsulm , no, its just me being stupid. The actual total blocks to transfer worked out at 52769, the progress bar is updating and showing percentages of upload. I'll add a text field to show the actual blocks uploaded.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote on 20 May 2021, 07:15 last edited by
          #4

          I guess the part of the code you have shown here is correct. However, it is hard to tell how that works together with the rest of your code.

          First of all, you need to make sure that nothing else is executed inside the main thread (i.e. the GUI thread). Also, you need to make sure that the timer lives inside the GUI thread as you are calling GUI functions (setValue() and update()). I believe that thhe call to update() is superfluous as setValue() will do the correct thing on its own already. It might be that updating every 10ms is too often. What I experienced so far, was that I was updating the progress in every iteration of the working loop. But, the loop was too fast which made the progress bar/dialog unnecessarily slow. This is what might happen in a quite similar way here.

          In general, I would suggest to update the progress dialog no more than every 50ms. This is fast enough for the general human. One thing to consider (you haven't provided the value of muint16Total) is to only call setValue() when the percentage changes as GUI stuff is quite expensive:

          QObject::connect(mptmrUpload, &QTimer::timeout, [this]() {
              if ( muint16Chunk < muint16Total &&
                   (100*muint16Chunk)/muint16Total != (100*(muint16Chunk+1))/muint16Total ) {
                 mpui->pgrbrStatus->setValue(++muint16Chunk);
                 //mpui->pgrbrStatus->update(); // I don't think you need this
              }
          });
          
          S 1 Reply Last reply 20 May 2021, 07:24
          0
          • S SimonSchroeder
            20 May 2021, 07:15

            I guess the part of the code you have shown here is correct. However, it is hard to tell how that works together with the rest of your code.

            First of all, you need to make sure that nothing else is executed inside the main thread (i.e. the GUI thread). Also, you need to make sure that the timer lives inside the GUI thread as you are calling GUI functions (setValue() and update()). I believe that thhe call to update() is superfluous as setValue() will do the correct thing on its own already. It might be that updating every 10ms is too often. What I experienced so far, was that I was updating the progress in every iteration of the working loop. But, the loop was too fast which made the progress bar/dialog unnecessarily slow. This is what might happen in a quite similar way here.

            In general, I would suggest to update the progress dialog no more than every 50ms. This is fast enough for the general human. One thing to consider (you haven't provided the value of muint16Total) is to only call setValue() when the percentage changes as GUI stuff is quite expensive:

            QObject::connect(mptmrUpload, &QTimer::timeout, [this]() {
                if ( muint16Chunk < muint16Total &&
                     (100*muint16Chunk)/muint16Total != (100*(muint16Chunk+1))/muint16Total ) {
                   mpui->pgrbrStatus->setValue(++muint16Chunk);
                   //mpui->pgrbrStatus->update(); // I don't think you need this
                }
            });
            
            S Offline
            S Offline
            SPlatten
            wrote on 20 May 2021, 07:24 last edited by
            #5

            @SimonSchroeder , this has been resolved, see my post before yours. Thank you for you post.

            Kind Regards,
            Sy

            1 Reply Last reply
            0

            4/5

            20 May 2021, 07:15

            • Login

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