Results 1 to 9 of 9

Thread: How reliable are QTimers on Windows?

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How reliable are QTimers on Windows?

    Hello,

    I would like to execute a function on a Windows machine as close to every 12 ms as somehow possible. Right now I'm using a QTimer and I'm not sure how well it does the job. The following test setup:

    Qt Code:
    1. constructor
    2. {
    3. stepTimer = new QTimer(this);
    4. connect(stepTimer,SIGNAL(timeout()), this, SLOT(tick()));
    5. stepTimer->start(12);
    6.  
    7. time = new QTime();
    8. time->start();
    9. }
    10.  
    11. void tick()
    12. {
    13. qDebug() << time->restart();
    14. }
    To copy to clipboard, switch view to plain text mode 

    shows that the tick() function is executed every 15 or 16 milliseconds. The output is like:

    15,16,15,16,15,16,15,16,15,16,15,16,15,16....

    When I start the timer with 10 ms instead of 12, I get:

    0,16,15,0,16,15,0,16,15,0,16,15,0,16,15...

    This is not exactly satisfactory.


    First of all, how exact are the timings that QTime::restart() is showing me?

    Of course it's very clear that tick() should execute in less than 12 ms and I believe qDebug() does so, but I better ask than be wrong?

    I know that Windows is not a real time OS and my own experiments show that it delivers a precision somewhere around the 10 ms mark. Does anyone have more exact information?

    And last but not least, what can I do to get the 12 ms rhythm as steady as possible?

  2. #2
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How reliable are QTimers on Windows?

    QTimer is absolutely not suited for that job. (It works on the event queue)

    Quote Originally Posted by Cruz View Post
    And last but not least, what can I do to get the 12 ms rhythm as steady as possible?
    This is the easiest to answer: You cant, because as you noticed it is not a real time operating system.

    The nearest you can get is to write a (hight priority) windows device driver....
    Last edited by seneca; 20th February 2009 at 13:05.

  3. #3
    Join Date
    Jan 2009
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How reliable are QTimers on Windows?

    Quote Originally Posted by Cruz View Post
    And last but not least, what can I do to get the 12 ms rhythm as steady as possible?
    I'd run up a seperate thread and lock that one instead of trying to go through an event loop.

  4. #4
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How reliable are QTimers on Windows?

    I believe the smallest time you can respond to anyway on most OS's is 50ms. Anyone disagree with this?

  5. #5
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How reliable are QTimers on Windows?

    Quote Originally Posted by stevey View Post
    I believe the smallest time you can respond to anyway on most OS's is 50ms. Anyone disagree with this?
    You can absolutely except better precision than that. As someone suggested I would set up a separate thread which polls the time and sleeps. Do a google search for "high performance timer".
    http://frank.mtsu.edu/~csjudy/direct...anceTimer.html

  6. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How reliable are QTimers on Windows?

    Regarding the suggestion of using a seperate thread, why should I do that? Is a sleep based control more precise, than a timer based control? My guts say that it isn't, because you never now when a thread will be woken up again and it might have slept too long.

    I think the trade off between having a faster timer and the complication plus windows dependency of the code is not worth it for me. I can live with 16 ms as long as the rhythm is steady. The most important criterion is that every iteration should take an equal amount of time.

    So the question that hasn't been addressed yet is, how precise are the timings that I get from QTime? When it says 16 ms, has it really been 16 ms? Or something between 10 and 20?

  7. #7
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How reliable are QTimers on Windows?

    Quote Originally Posted by Cruz View Post
    So the question that hasn't been addressed yet is, how precise are the timings that I get from QTime? When it says 16 ms, has it really been 16 ms? Or something between 10 and 20?
    On XP+ between 10 and 20 ms. on NT between 0 and 50 ms, assuming the GUI thread isn't too busy.

  8. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How reliable are QTimers on Windows?

    Hm, here it says that QTimer now uses the multimedia timer API on Win XP and it should have a 1 ms resolution. That sounds good actually. But then how come that my measurements in my OP report 16 ms instead of 12? Could it be that the gui thread takes that long to process events before it can fire the timer? Or is it because the answer from Qtime::restart() is not precise?

  9. #9
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How reliable are QTimers on Windows?

    You should try to understand the difference between windows and a real time operation system, for example QNX. No matter how hard you try, an application will NEVER be able to answer or process any request exactly per millisecond:

    Besides your application, many other applications and system processes are running at same time sharing all the same processor and memory, and the scheduler will assign time to them in a non-tranparent way (non-transparent for the application). There is absolutely no guarantee that your application will get any processing time at all between two of your ticks. This is something beyond the scope of Qt (or any other application framework).

Similar Threads

  1. Qt Jambi, deploying app on Mac & Windows fails
    By ChrisColon in forum Installation and Deployment
    Replies: 2
    Last Post: 16th February 2009, 22:05
  2. Replies: 5
    Last Post: 15th January 2009, 09:03
  3. Opening text file fails after autostartup on windows
    By yogourta in forum Qt Programming
    Replies: 2
    Last Post: 18th October 2008, 03:52
  4. converting unix exe to windows binary
    By deekayt in forum General Programming
    Replies: 2
    Last Post: 17th September 2006, 01:00
  5. Qt and windows vista
    By munna in forum General Discussion
    Replies: 8
    Last Post: 11th January 2006, 22:33

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.