Results 1 to 9 of 9

Thread: Qtimer accuracy

  1. #1
    Join Date
    Apr 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Qtimer accuracy

    Hi
    My app is controlling two pumps via RS-232 (qextserialport). The pumps should be updated with a new rate every second. It works fine but when I compare the elapsed time in my qt-app with an accurate stop-watch it differs approximately 1 second per minute. The Qtimer seems to be slower than the stop-watch.

    It is even worse (slower) when I controll two pumps from the same app so I need to controll each pump from one software each. Having two apps open is not a big problem the biggest problem is that the pumps are running for about 24-48 hours before stop and adding all differences in Qtimer means that after 24 hours the app has only pumped for about 23.5 hours whilst the timer says 24 h. Are there any Qtimer with more accuracy or depends this problem on the machine (HP-Laptop quite new)

    Thanksfor any help!
    Thomas

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

    Default Re: Qtimer accuracy

    Hello Thomas!

    It could be related to system timer resolution, which is only set to a high setting, when you use intervals below 20ms. Try this:

    Qt Code:
    1. YourClass::YourClass(..)
    2. {
    3. QTimer* timer = new QTimer();
    4. connect(timer,SIGNAL(timeout()),this,SLOT(doSomething());
    5. timer->start(10);
    6. }
    7.  
    8. void YourClass::doSomething()
    9. {
    10. static int counter = 1;
    11. if (counter == 100)
    12. {
    13. // do your stuff here..
    14.  
    15. counter = 1;
    16. } else {
    17. ++counter;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    HIH

    Johannes

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Qtimer accuracy

    Probably your problem is caused by noneffective code. Remember that timer event is processed only when event loop is reached, so your code must often return control to event loop. From time event to time event you can have small inaccuracies caused by this and they can accumulate to bigger differences.
    If you need long term accuracy I wouldn't depend on QTimer only. Maybe you should try to use QTime::elapsed or QElapsedTimer to be able to compensate that differences.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qtimer accuracy

    First : from QTimer doc : Timers will never time out earlier than the specified timeout value and they are not guaranteed to time out at the exact value specified. In many situations, they may time out late by a period of time that depends on the accuracy of the system timers.

    Second : Windows is not real time OS.
    You must remember this and, I think, redesign application.

    P.S. How did You measure time with QTimer ?

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qtimer accuracy

    Windows does have high resolution timers available but using them will vastly increase your CPU usage.

    A better idea would be to query the system time say once a minute and recalibrate your application based on the result.

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

    Default Re: Qtimer accuracy

    @squidge: Out of curiosity: What sort of timers are you talking about?

    And how do they affect CPU usage? I have heard about this, but never saw any increase in CPU load.

    Joh

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qtimer accuracy

    Basically, the timer you are using now lets your process go to sleep and lets it wake up when its time to process messages. Your timer will be one of these messages.

    If you use the high resolution timers such as QueryPerformanceCounter(), then you need to busy loop to get the best results, and so your CPU usage goes through the roof compared to the message based approach.

    What you could do as a half/half solution would be use the message based timers, and then when you get a time update, figure out the time difference between two calls to QueryPerformanceCounter() and use that as the actual increment.

    Note that you'll also need to use QueryPerformanceFrequency() in your application initialisation as QueryPerformanceCounter() only works in counts.

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

    Default Re: Qtimer accuracy

    Ah. I'm quite familiar with performance counter, and especially the very inaccurate QueryPerformanceFrequency. If you call this on two computers they will probably return the same nominal frequency, but can deviate a lot in actual counts. I just wouldn't call it a timer. Thats why I asked.

    Joh

  9. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qtimer accuracy

    Theres always multimedia timers, too, but for a lot of projects, combining message-based timers with high resolution timers is a better approach.

    Have a look at CreateTimerQueueTimer()

Similar Threads

  1. frame rate and QTimer accuracy
    By totem in forum Qt Programming
    Replies: 13
    Last Post: 5th April 2011, 16:26
  2. Accuracy of the timer in Qt
    By Fastman in forum Qt Programming
    Replies: 5
    Last Post: 21st July 2009, 21:52
  3. Current Time in msec accuracy in Qt for WinCE
    By funghy in forum Qt Programming
    Replies: 4
    Last Post: 22nd June 2009, 10:00
  4. accuracy
    By mickey in forum General Discussion
    Replies: 0
    Last Post: 13th May 2009, 23:05
  5. QPainterPath::arcTo accuracy
    By robertson1 in forum Qt Programming
    Replies: 0
    Last Post: 18th July 2008, 12: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.