Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qtimer on Windows. What's wrong with it???

Qtimer on Windows. What's wrong with it???

Scheduled Pinned Locked Moved General and Desktop
9 Posts 7 Posters 9.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aureshinite
    wrote on last edited by
    #1

    Qtimer seems to not be very precise on Windows. I need a time delay between 10ms and 1s, and depending on the interval, sometimes the delay is even doubled. Why is that??

    For example,on my win xp the test below gives an average delay of:

    • 47 ms when I set the argument to 33ms
    • 12/13 ms for 13ms
    • 93 for anything between 80 and 93
    • I was tired and decided that these values were alarming enough to stop there :(

    @
    #include <QDebug>
    #include <QTime>
    #include <QTimer>
    #include <QWidget>
    class WriteTime: public QWidget
    {
    Q_OBJECT

    private:
    int shootinterval;
    QTime chrono;
    QTimer timer;
    int count;

    public:
    explicit WriteTime(QWidget* parent, int interval)
    :QWidget(parent)
    , shootinterval(interval)
    , chrono()
    , timer()
    , count(0)
    {
    connect(&timer,SIGNAL(timeout()),this,SLOT(updateStuff()));
    }

    void start(){
         timer.setInterval(shootinterval);
         timer.start();
         chrono.start();
    }
    

    private slots:
    void updateStuff(){
    count++;
    if(count == 100){
    qDebug()<< chrono.elapsed()/100;
    chrono.restart();
    count = 0;
    }
    }

    };

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    WriteTime t(0,/whatever you want here/);
    t.start();
    return a.exec();

    }
    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      windows is not a realtime OS. A timer is just an event that is emitted. It will be processed by the event loop when the event loop is idle and no other thread is currently active. You can't rely on getting this directly in time. Not on windows and also not on Linux nor Mac.

      So your PC is executing other apps and your process also does other stuff (updating UI, printing debug messages, ...)
      As you use qDebug for getting the messages, I assume you have the debugger connected, which also has influence on that. You can rely, that the event is send at the correct time, but when it is handled is a total different thing.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Gerolf is right. Further more, especially windows has a limited resolution for the timers anyway. You simply can not get 1ms accuracy.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          1ms accuracy is a challenge even on Unix boxes. The simple fact is that for real-life desktop applications you never actually should have to bother with this.

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            Think of QTimer as to only guarantee that the timeout triggers not before the set interval :-) (or at the fastest opportunity possible in case of timeout = 0).

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aureshinite
              wrote on last edited by
              #6

              thanks for your replies.
              I don't really need 1ms , usually my lowest timer resolution would be 15ms.
              I use qDebug() but the debugger is disconnected. I tested it with QCreator so the messages are displayed.

              Well this test program is far from perfect, I know that.
              I understand the notion of preemption, and the program above has only my QTImer::timeout() signal in the event loop most of the time. But the difference in behavior is quite high between the Windows and Linux implementation.

              I have tried something else:

              • I increment the timer value after some amount of iterations
              • On linux I get incremental values, sometimes bigger than the timeout as you guys mentioned.
                I have to draw the results it will be like a LINE CHART
              • On windows, the results are more like a CLASS HISTOGRAM: between [x,y] i got y;
              1 Reply Last reply
              0
              • F Offline
                F Offline
                Franzk
                wrote on last edited by
                #7

                15 ms is already tricky. Windows systems can't be more precise than 10 to 16 ms at best. Linux desktop systems usually can make 1 ms.

                "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  code_fodder
                  wrote on last edited by
                  #8

                  I know this is an old topic now, but there is no point in me opening a new topic when all the information is already here. But I have a further observation and question:

                  I get very similar numbers to those from the first post, my timer is aimed at triggering every 60ms , but it hits either of the following times (and no others):

                  47ms, 62/63ms, 78ms

                  The other figure from the original post is 93ms.

                  Each of these are 15/16ms apart. It seems that windows has some sort of time-slots it is using, does anyone know about that? - it can't be just coincidence that we are always hitting these numbers?

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    eatenbyagrue
                    wrote on last edited by
                    #9

                    I don't think this is necessarily a Windows issue, vs. an issue with Qt's implementation of their timer. Windows is apparently capable of high resolution timers - more info here http://msdn.microsoft.com/en-us/library/windows/desktop/ms644900(v=vs.85).aspx

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved