Results 1 to 14 of 14

Thread: frame rate and QTimer accuracy

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: frame rate and QTimer accuracy

    Hi High_flyer!

    You are absolutely right! The setting for the high resolution timer is done only by the timeBeginPeriod call.

    And the privilege adjustment is not required for it! I didn't realize! It was only in context of an NTP-Clock sync, that this made sense.

    But as I mentioned none of it is required to have a 1 ms QTimer work correctly. Only if you want QThread::sleep to work accurately you will need to set the timer resolution yourself.
    I just mentioned it, because I had trouble with it in context of high precision timing.

    Joh

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: frame rate and QTimer accuracy

    But as I mentioned none of it is required to have a 1 ms QTimer work correctly. Only if you want QThread::sleep to work accurately you will need to set the timer resolution yourself.
    So just to see if I understand what you are saying is that for having QThread::sleep() work accurately, one must set the windows timer resolution (per process) via timeBeginPeriod() + timeEndPeriod(), is that correct?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: frame rate and QTimer accuracy

    Yes, this is correct for Windows. I experienced that on WinXPSP3,Win7-32 and 64bit. The timer resolution equals the kernels schedulers granularity so unless you set it to 1ms you can't get accurate sleep intervals.

    To prove it, I hacked together a small test application:

    It runs msleep(1) 100x times. As the default timer resolution of windows is 10ms, you get about 1sec for 100 calls to msleep(1):

    Qt Code:
    1. 999
    2. 998
    3. BeginPeriod!
    4. 110
    5. 94
    6. 110
    7. EndPeriod!
    8. 998
    9. 1014
    To copy to clipboard, switch view to plain text mode 
    Before you run it, make sure all other processes that might request a higher timer resolution are closed. This includes winamp, your browser (flash plugin!), ..
    You can then check the current timer resolution with sysinternals clockres.

    Here is the sourcecode:

    Qt Code:
    1. main.h
    2. #ifndef MAIN_H
    3. #define MAIN_H
    4.  
    5. #include <QtGui>
    6.  
    7. #ifdef Q_OS_WIN
    8. #include "windows.h"
    9. #endif
    10.  
    11. class SleeperThread : public QThread
    12. { Q_OBJECT
    13. public:
    14. SleeperThread(int interval,QObject* parent = 0) : QThread(parent), m_interval(interval) {}
    15. protected:
    16. virtual void run()
    17. {
    18. QElapsedTimer timer;
    19. timer.start();
    20. for (int i=0;i<100;++i)
    21. {
    22. msleep(m_interval);
    23. }
    24. QMetaObject::invokeMethod(parent(),"showResult",Q_ARG(int, timer.elapsed()));
    25. }
    26. private:
    27. int m_interval;
    28. };
    29.  
    30. class MainWindow : public QMainWindow
    31. { Q_OBJECT
    32. public:
    33. MainWindow()
    34. {
    35. QPushButton* start_pb = new QPushButton("Test Sleep");
    36. connect(start_pb,SIGNAL(clicked()),this,SLOT(testSleep()));
    37.  
    38. QPushButton* beginPeriod_pb = new QPushButton("Begin Period");
    39. connect(beginPeriod_pb,SIGNAL(clicked()),this,SLOT(beginPeriod()));
    40. QPushButton* endPeriod_pb = new QPushButton("End Period");
    41. connect(endPeriod_pb,SIGNAL(clicked()),this,SLOT(endPeriod()));
    42.  
    43. QHBoxLayout* hl = new QHBoxLayout();
    44. hl->addWidget(beginPeriod_pb);
    45. hl->addWidget(start_pb);
    46. hl->addWidget(endPeriod_pb);
    47.  
    48. output_te = new QPlainTextEdit();
    49. output_te->setReadOnly(true);
    50.  
    51. QVBoxLayout* vl = new QVBoxLayout();
    52. vl->addLayout(hl);
    53. vl->addWidget(output_te);
    54.  
    55.  
    56. QWidget* w = new QWidget();
    57. w->setLayout(vl);
    58.  
    59. setCentralWidget(w);
    60. }
    61. public slots:
    62. void testSleep()
    63. {
    64. SleeperThread* st = new SleeperThread(1,this);
    65. connect(st,SIGNAL(result(int)),this,SLOT(showResult(int)));
    66. connect(st,SIGNAL(finished()),st,SLOT(deleteLater()));
    67. st->start();
    68. }
    69.  
    70. void beginPeriod()
    71. {
    72. output_te->appendPlainText("BeginPeriod!");
    73. #ifdef Q_OS_WIN
    74. if (timeBeginPeriod(1) == TIMERR_NOCANDO)
    75. qWarning() << "Could not start the time period!";
    76. #endif
    77. }
    78.  
    79. void endPeriod()
    80. {
    81. output_te->appendPlainText("EndPeriod!");
    82. #ifdef Q_OS_WIN
    83. timeEndPeriod(1);
    84. #endif
    85. }
    86.  
    87. void showResult(int elapsed)
    88. {
    89. output_te->appendPlainText(QString::number(elapsed));
    90. }
    91. private:
    92. QPlainTextEdit* output_te;
    93. };
    94.  
    95. #endif // MAIN_H
    96.  
    97. main.cpp:
    98.  
    99. #include <QApplication>
    100. #include <QtGui>
    101.  
    102. #include "main.h"
    103.  
    104. int main(int argc, char** argv)
    105. {
    106. QApplication app(argc,argv) ;
    107.  
    108. MainWindow mw;
    109. mw.show();
    110.  
    111. return app.exec();
    112. }
    To copy to clipboard, switch view to plain text mode 
    Johannes
    Last edited by JohannesMunk; 5th April 2011 at 13:58. Reason: Changed ClockRes link to technet en-us

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

    high_flyer (5th April 2011)

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: frame rate and QTimer accuracy

    for the sake if completeness, here is a good link I just found on the subject:
    http://msdn.microsoft.com/en-us/magazine/cc163996.aspx
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #5
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: frame rate and QTimer accuracy

    Actually the article is not about a timer that can periodically schedule or delay something, but about using the Performance Counter API to supplement the precision of the system time and thus interval measurement. They explain the lack of resolution by the lack of resolution of the system timer, though.

    Joh


    Added after 9 minutes:


    To get back on the original topic, I just tested, that when you start a QTimer with an arbitrary number above 20ms it will not adapt the timer resolution. So that a timer of 33ms will fire either after 30ms or 40ms, because the system timer resolution is at 10ms.

    That problem can be resolved as i suggested by using a 1ms timer and counting its timeouts or probably (I didn't check this) by using a 33ms timer and manually setting the windows timer resolution to 1ms with timeBeginPeriod!

    Joh
    Last edited by JohannesMunk; 5th April 2011 at 14:34.

  7. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: frame rate and QTimer accuracy

    They explain the lack of resolution by the lack of resolution of the system timer, though.
    Yes, but also how to increase the timer resolution in the limits of a given system, which I think is what the OP was generally after.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #7
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: frame rate and QTimer accuracy

    When I look at the similar threads below this seems to be a rather common problem.

    http://www.qtcentre.org/threads/2316...is-not-acurate

    Maybe I should create a WIKI-Article about precision timing under windows sometime. I have spent quite a lot of time with performance counters and their unreliable frequency statements and with thread-switching times, due to a real time data acquisition suite i'm currently developing.

    Joh

  9. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: frame rate and QTimer accuracy

    Maybe I should create a WIKI-Article about precision timing under windows sometime. I have spent quite a lot of time with performance counters and their unreliable frequency statements and with thread-switching times, due to a real time data acquisition suite i'm currently developing.
    I am not going to stop you!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #9
    Join Date
    Dec 2009
    Posts
    128
    Platforms
    Unix/X11 Windows
    Thanks
    7
    Thanked 14 Times in 14 Posts

    Default Re: frame rate and QTimer accuracy

    Quote Originally Posted by JohannesMunk View Post
    The setting for the high resolution timer is done only by the timeBeginPeriod call.

    And the privilege adjustment is not required for it! I didn't realize! It was only in context of an NTP-Clock sync, that this made sense.

    But as I mentioned none of it is required to have a 1 ms QTimer work correctly. Only if you want QThread::sleep to work accurately you will need to set the timer resolution yourself.
    I just mentioned it, because I had trouble with it in context of high precision timing.
    Joh
    So If I don't use QThread::sleep I don't need to include windows-specific code ?
    I'd be glad because it makes me link against some MS lib (i got linker error with timeBeginPeriod call I did not even tried to resolve)

    By the way I tried your solution and it seems to work for me, thanks again.
    As I said I needed a frame interval of 33ms so I used a timer interval of 11ms, which implies an update every 3 timer events. Now animation duration seems consistent

  11. #10
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: frame rate and QTimer accuracy

    Hi Totem!

    Yes, below 20ms QTimer-interval, windows timer resolution is set to 1ms, without you doing anything.
    Thats why your 11ms should be quite accurate.

    Glad that your problem is resolved,

    Happy coding,

    Johannes

Similar Threads

  1. OpenGL frame rate
    By martinb0820 in forum Installation and Deployment
    Replies: 3
    Last Post: 2nd October 2009, 20:24
  2. OpenGL frame rate under 64-bit XP
    By martinb0820 in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2009, 20:23
  3. Replies: 24
    Last Post: 13th August 2009, 11:13
  4. Frame rate problem
    By MrShahi in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2008, 00:06
  5. Change frame rate
    By superutsav in forum General Programming
    Replies: 1
    Last Post: 3rd August 2006, 22:02

Tags for this Thread

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.