1

I am trying to catch how fast a number is being rotated. I have a program where the counter starts at 0.
Every time the user clicks the button "add 1". It increments the counter by 1.
The counter maxes at 255. after 255 it goes back to 0.
Now I want to display how many seconds it took from 0 to increment to max and back to 0. Time for the complete revolution. Its gonna differ on how fast the user has clicked the button.

What I need to know is how to use a timer for this? The program is complete just the timer between the complete revolution of the numbers I need to code.

I'm using

#include <time.h>    //* clock_t, clock, CLOCKS_PER_SEC

Please advice. Thank you.

int counter;
counter = pkt[0];

cout << endl;

cout << counter << endl;

if(counter == 1)
{
    cout << "revolution" << endl;
}
madziikoy
  • 1,447
  • 7
  • 22
  • 32
  • used times. clock() function. but it doesn't give me what I'm looking for. – madziikoy Mar 13 '13 at 08:59
  • [Possible duplicate](http://stackoverflow.com/q/307596/1084416) – Peter Wood Mar 13 '13 at 08:59
  • 1
    @madziikoy You probably don't want `clock`, since that gives you (or is supposed to give you) CPU time, not wall clock time. `time()` will return wall clock time, and `difftime` will tell you the interval between the two wall clock times. – James Kanze Mar 13 '13 at 09:05

3 Answers3

3

If you can use C++11, <chrono> could be a good choice.

It contains:

Durations

They measure time spans, like: one minute, two hours, or ten milliseconds. In this library, they are represented with objects of the duration class template, that couples a count representation and a period precision (e.g., ten milliseconds has ten as count representation and milliseconds as period precision).

Time points

A reference to a specific point in time, like one's birthday, today's dawn, or when the next train passes. In this library, objects of the time_point class template express this by using a duration relative to an epoch (which is a fixed point in time common to all time_point objects using the same clock).

Clocks

A framework that relates a time point to real physical time. The library provides at least three clocks that provide means to express the current time as a time_point: system_clock, steady_clock and high_resolution_clock.

888
  • 3,246
  • 8
  • 41
  • 60
0

difftime may be used to get the difference between two time instances. difftime The example code on the page gives what you are looking for.

uba
  • 2,012
  • 18
  • 21
  • Yes, I tried but I want the a timer to start when the counter is at 0 and stop ay 255. And calculate how long it took. – madziikoy Mar 13 '13 at 09:00
  • 1
    "_I tried_" Well, **what** did you try? No tried code is available in your question. – uba Mar 13 '13 at 09:02
0
#include <ctime>
time_t start = time(NULL);

will give you the number of seconds that have passed since 00:00 hours, Jan 1, 1970 UTC. If you put that at the start of the code you want to keep track of, you simply need to get the time again when you want to calculate how long it took to perform a complete revolution. Then get the difference. difftime can help you get the difference. Don't think of it as a timer that's running really. You're just getting the time before and after the revolution and finding the difference.

Memento Mori
  • 3,327
  • 2
  • 22
  • 29