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?

Why is timer so slow?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 565 Views
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on 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

    jsulmJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      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);
      
      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on 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

      SPlattenS 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on 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 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
              }
          });
          
          SPlattenS 1 Reply Last reply
          0
          • S SimonSchroeder

            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
                }
            });
            
            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on 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

            • Login

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