Results 1 to 5 of 5

Thread: QTimer is slowing down

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QTimer is slowing down

    At first I know that Windows is not realtime system, so I really do not expect that QTimer will have some bigger accuracy. But I can see that my QTimer is slowing down from 200ms timoeout to 500ms timeout in ~7minutes.
    I am using debug output and timing stamps so I really can confirm that QTimer is slowing down. I am using like this:

    Qt Code:
    1. void timeoutFnc() {
    2. stopTimer();
    3. /* doing something (it can takes 1s) */
    4. startTimer(200);
    5. }
    To copy to clipboard, switch view to plain text mode 

    So I need from each run 200ms distance. My idea is that maybe it is not good to stop and start QTimer so many times. But I need this behaviour.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTimer is slowing down

    Provide a minimal, complete program that demonstrates the problem. This, for example, shows no such issue:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. class Thingy: public QObject {
    4. Q_OBJECT
    5. QTimer timer;
    6. QTime stopwatch;
    7.  
    8. public:
    9. explicit Thingy(QObject *p = 0): QObject(p) {
    10. connect(&timer, SIGNAL(timeout()), SLOT(doSomethingLong()));
    11. timer.setInterval(200);
    12. timer.start();
    13. stopwatch.start();
    14. }
    15.  
    16. private slots:
    17. void doSomethingLong() {
    18. qDebug() << "Enter" << stopwatch.elapsed();
    19. timer.stop();
    20.  
    21. // Simulate long runner
    22. qDebug() << " Doing stuff";
    23. QEventLoop loop;
    24. QTimer t;
    25. t.singleShot(1000, &loop, SLOT(quit()));
    26. loop.exec();
    27.  
    28. timer.start();
    29. qDebug() << "Leave" << stopwatch.elapsed();
    30. }
    31. };
    32.  
    33. int main(int argc, char **argv) {
    34. QCoreApplication app(argc, argv);
    35. Thingy t;
    36. return app.exec();
    37. }
    38. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    I get 200 milliseconds or 201 milliseconds interval consistently. Total drift over 7 minutes of 23 milliseconds on Linux but more like 400 or 500 milliseconds on Windows.
    Last edited by ChrisW67; 9th October 2013 at 09:54.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer is slowing down

    Do not expect a timer to fire every 200ms if in the meantime you're occupying the program for more than 200ms without returning to the event loop.

    Furthermore QTimer does not guarantee that it fires each 200ms (or whatever you set the timeout to), it guarantees that the timespan between two consecutive timeouts will be not less than 200ms (or whatever you set the timeout to). So if one trigger is late by x ms (because of whatever reason) then all consecutive triggers will be late by not less than x ms. You need to correct the skew yourself if you want a regular timespan between timeouts.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    saman_artorious (9th October 2013)

  5. #4
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTimer is slowing down

    to wysota: I know, why you think that I wrote 'At first I know that Windows is not realtime system'. Problem is that I'm stoping timer and starting again and the timeout is every second bigger.

    Problem is that it is no matter what I am doing when I stop timer and start again. For me for example I am sending data to COM Serial bus. But if I start again timer, next timeout is every second more bigger then 200ms which I want. After 7minutes it is about 400-500ms. Does not matter what I am doing on Windows it is always the same behaviour. It will be ok if timeout will be from 200 to 300 maybe also to 400ms but after one minute when program is running it is about 1000ms. It is growing.
    Last edited by Lodhart; 9th October 2013 at 11:22.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTimer is slowing down

    Quote Originally Posted by Lodhart View Post
    to wysota: I know, why you think that I wrote 'At first I know that Windows is not realtime system'.
    A real time system has nothing to do with this. I'm talking purely about QTimer semantics.

    Problem is that it is no matter what I am doing when I stop timer and start again. For me for example I am sending data to COM Serial bus. But if I start again timer, next timeout is every second more bigger then 200ms which I want. After 7minutes it is about 400-500ms. Does not matter what I am doing on Windows it is always the same behaviour. It will be ok if timeout will be from 200 to 300 maybe also to 400ms but after one minute when program is running it is about 1000ms. It is growing.
    Show us your code which performs what you have written here in a form of a minimal compilable example.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 19:47
  2. Replies: 15
    Last Post: 4th August 2012, 19:11
  3. QProgressDialog is slowing down loop?
    By marwooj in forum Qt Programming
    Replies: 9
    Last Post: 29th June 2011, 08:24
  4. QTimer
    By dragon in forum Qt Programming
    Replies: 18
    Last Post: 16th November 2008, 14:15
  5. QTimer
    By nirup in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2007, 14:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.