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. Solved: time measurement
Forum Updated to NodeBB v4.3 + New Features

Solved: time measurement

Scheduled Pinned Locked Moved General and Desktop
12 Posts 6 Posters 11.4k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    romeo.rbw
    wrote on 12 Oct 2012, 03:19 last edited by
    #1

    Hi...
    Is there any function or method to calculate time in millisecond precisely in Qt? The application is to measure time between click of two targets.
    Thank you.

    romeo

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 12 Oct 2012, 06:09 last edited by
      #2

      Yes, but only for a given value of "precisely". A lovely class named "QTimer":http://qt-project.org/doc/qt-4.8/QTimer.html should be your friend here.

      EDIT: Actually in your case, you should take a look at "QTime":http://qt-project.org/doc/qt-4.8/QTime.html.

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 12 Oct 2012, 07:16 last edited by
        #3

        [[doc:QElapsedTimer]] should be your friend. But: the precision is limited. What precision are you looking for exactly?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          romeo.rbw
          wrote on 12 Oct 2012, 07:40 last edited by
          #4

          The precision around 1 ms is acceptable.
          From the manual I quote: "QElapsedTimer will always use the same clock, so it's safe to compare with the value coming from another process in the same machine". May be it is appropriate for testing/evaluation process that is for my application. Is it okay?
          Thanks for every reply...

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 12 Oct 2012, 08:01 last edited by
            #5

            I doubt you're going to get anywhere near 1ms resolution. Most systems don't provide a clock that fast (or accurate) by default, and then there is the way clicks are actually processed. They are picked up by QApplication, who creates QEvent instances that get posted to the event loop. From the event loop, they are processed by sending them to their targets one by one. That means that if you are already doing some work, or if one of the other events in the queue triggers a bit of work to be done, the delivery of your mouse event will be delayed. There goes your 1ms accuracy out of the window...

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on 12 Oct 2012, 11:59 last edited by
              #6

              Nevermind.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DerManu
                wrote on 12 Oct 2012, 21:33 last edited by
                #7

                If your timing hardware is up to it (which is mostly the case nowadays), you can get up to microsecond accuracy with gettimeofday (linux) or QueryPerformanceCounter (Windows). But those aren't Qt functions, you'd need to write a wrapper yourself.
                Note that Andres post basically tells you the limit in your application: the event queue system and thread time-slicing (~20ms) of the operating system. So the functions above are only of use to measure the time between the execution of the respective code lines, e.g. to measure performance, and not in your case to measure physical events that get queued by the OS before they reach you.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on 13 Oct 2012, 10:13 last edited by
                  #8

                  [quote author="DerManu" date="1350077601"]If your timing hardware is up to it (which is mostly the case nowadays), you can get up to microsecond accuracy with gettimeofday (linux) or QueryPerformanceCounter (Windows). But those aren't Qt functions, you'd need to write a wrapper yourself.[/quote]This is exactly what QElapsedTimer does.

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    utcenter
                    wrote on 13 Oct 2012, 10:54 last edited by
                    #9

                    I've had problems with the readings of QElapsedTimer, then I found out that the good old "clock()":http://www.cplusplus.com/reference/clibrary/ctime/clock/ function from the C standard library works very well if you don't need more than millisecond resolution. There might be implementations that return actual processor clocks but the ones I've used return miliseconds - mingW, MSVC, ICC. This is a good thing since modern processors change their running frequencies all the time and even if you had raw clocks there is no way to calculate the actual time those took. Back in the days of C processors were running on a fixed clock speed.

                    @
                    int start = clock();
                    ... do some work
                    int time = clock() - start;@

                    time should be the time in msec

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DerManu
                      wrote on 13 Oct 2012, 11:15 last edited by
                      #10

                      [quote author="Lukas Geyer" date="1350123216"]This is exactly what QElapsedTimer does.
                      [/quote]

                      Only since 4.8. In 4.7 it only provided millisecond accuracy, unfortunately.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        romeo.rbw
                        wrote on 17 Oct 2012, 01:52 last edited by
                        #11

                        Thanks for every suggestion and knowledge, my app connecting to openCV library too. I found cv::gettickcount and cv::gettickfrequency. The model is the same like utcenter's comment in C. I will explore more or may be someone already tried it?

                        Thank you

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          romeo.rbw
                          wrote on 25 Oct 2012, 05:35 last edited by
                          #12

                          It works well using utcenter's comment, better than using cv::gettickcount and cv::gettickfrequency in my case.
                          Thanks.

                          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