Why is timer so slow?
-
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);
-
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);
@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.
-
@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.
-
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()
andupdate()
). I believe that thhe call toupdate()
is superfluous assetValue()
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 } });
-
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()
andupdate()
). I believe that thhe call toupdate()
is superfluous assetValue()
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 } });
@SimonSchroeder , this has been resolved, see my post before yours. Thank you for you post.