Need help with timer

Nov 24, 2011 at 4:19am
I been searching all over Google and nothing seems to be consistent/useful about timers.

Does anyone know how to get the current time? I want to be able to get the start time and another time and subtract them to get the difference or be able to run a loop for a certain time.
Nov 24, 2011 at 4:29am
Nov 24, 2011 at 4:37am
Isn't that for C?
Nov 24, 2011 at 4:42am
Current time, also known as system time or "wall time" can be adjusted by the user (or by the NTP client, or some other means) at any moment. It is not suitable for measuring intervals.

(but, for reference, it can be obtained with time(), system_clock::now(), or with various OS means, such as gettimeofday())

What is suitable for measuring duration of a loop is the monotonic time, time that can never be adjusted by the user, can never run faster or slower, and always increments at a steady rate. Such time can be obtained with clock(), steady_clock::now(), or with various OS means (clock_gettime() is a good one on Linux and QueryPerformanceCounter() on Windows).

Monotonic time has no meaning on its own, only the difference (value after loop minus value before loop) has a meaning.

references:
clock(): http://www.cplusplus.com/reference/clibrary/ctime/clock/
steady_clock::now() http://en.cppreference.com/w/cpp/chrono/steady_clock
Linux clock_gettime() http://linux.die.net/man/2/clock_gettime
Windows QueryPerformanceCounter() http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx

Last edited on Nov 24, 2011 at 4:43am
Nov 24, 2011 at 4:42am
I think he wants http://www.cplusplus.com/reference/clibrary/ctime/time
Yes, and no. They are from C, but are supported in C++, as are most things from C. Notice the <c...> prefix in the headers; like <cmath> and <cstdlib>. These are C++ headers for C constructs.
Nov 24, 2011 at 4:50am
I read all those, but I really have not a clue which to use to capture the interval. My main goal is to run the a loop for lets say 15 seconds. I would need the current time start and then I would have to make a loop to check if the difference between the current time now and current time start is still less than or equal to 15 seconds. I prefer something that will give me seconds over like clock, which I am not sure if it gives like 10:10 or 10:10:10.
Nov 24, 2011 at 5:16am
What is your problem (in short.) Maybe we can help you decide how to approach it.
Last edited on Nov 24, 2011 at 5:16am
Nov 24, 2011 at 5:06pm
Like based on the descriptions, I wasn't sure which did what. I wanted to measure intervals in seconds, so I wanted to see which of those functions would allow me to do such. Wasn't sure if those gave me the actual time like the form of hh:mm or hh:mm:ss or something different.
Nov 26, 2011 at 9:09am
time() measures ("real world") seconds. However, it's not in a nice format. I believe it returns a Unix time stamp (the number of seconds since 12:00 midnight UTC on January 1, 1970. http://en.wikipedia.org/wiki/Unix_time )

Since you need the measure of the difference in seconds between to times, it should work for you (even without it being a pretty number.)
Nov 26, 2011 at 4:58pm
Wow, that is ugly...

How about clock()?
Nov 26, 2011 at 7:04pm
It's not ugly...
1
2
3
4
time_t startTime = time();
//do stuff that takes awhile, at least a few seconds...
time_t endTime = time();
cout << "This is how long the program took: " << (endTime - startTime);

If you need to print something in a format, like HH:MM:SS, there are STL function for that I believe.
Last edited on Nov 26, 2011 at 7:07pm
Nov 26, 2011 at 7:32pm
I meant that it was the time since 1970, which is ugly.
Nov 26, 2011 at 7:43pm
So what year do you prefer?
Nov 26, 2011 at 10:12pm
I am fine with it. I assumed time would just give you the current time, not the time since 1970.
Nov 27, 2011 at 5:56pm
Okay, explain how you would do it if you wrote a time routine? Remember that a Unix Time Stamp is only 32 bits (4 bytes.) What I mean is, how would you define "the current time" unambiguously.
Topic archived. No new replies allowed.