Getting the current time in c++

Started by
8 comments, last by jflanglois 19 years ago
how do i get the current time (i read that the functions in time.h return the time in seconds, and i want a higher accuracy)?
Advertisement
I can't imagine why you would want the time-of-day that much resolution. After all, when you set the time on your computer, is it accurate to the millisecond?

I am assuming that you want to time how long something takes, such as frame rate. For that you need higher resolution of course. I don't believe there is a standard C function that measures time with better-than-seconds resolution (clock() perhaps?). Anyway, if you are programming for Windows, look up QueryPerformanceCounter().
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
I don't believe there is a standard C function that measures time with better-than-seconds resolution (clock() perhaps?).


Potentially. Although there's no guaranteed resolution, the value returned by clock() increases by CLOCKS_PER_SEC every second - so if CLOCKS_PER_SEC is 1000, clock() would return milliseconds. On my system it's 1,000,000.

For a C++ solution, you may want to use the Boost Timer Library - it depends on the functionality of clock().

Further information from my manpage:

NAME       clock - Determine processor timeSYNOPSIS       #include <time.h>       clock_t clock(void);DESCRIPTION       The clock() function returns an approximation of processor       time used by the program.RETURN VALUE       The value returned is the  CPU  time  used  so  far  as  a       clock_t;  to  get  the  number  of seconds used, divide by       CLOCKS_PER_SEC.  If the processor time used is not  avail­       able  or  its  value  cannot  be represented, the function       returns the value (clock_t)-1.CONFORMING TO       ANSI C.  POSIX requires that CLOCKS_PER_SEC equals 1000000       independent of the actual resolution.NOTES       The C standard allows for arbitrary values at the start of       the program; subtract the value returned from  a  call  to       clock()  at the start of the program to get maximum porta­       bility.       Note that the time can wrap around.   On  a  32bit  system       where  CLOCKS_PER_SEC  equals  1000000  this function will       return the same value approximately every 72 minutes.       The times() function call returns more information.SEE ALSO       getrusage(2), times(2)GNU                         1993-04-21                   CLOCK(3)
maybe i'm thinking about this the wrong way- i dont need the current date, just want to measure how much time has passed since the last game loop has ended so i'll how much distance to move each figure in the game...
Depending on the platform there are functions that provide the total CPU time. Windows, GLUT, and SDL all provide these functions. So you can either look them up or just tell us what you're using and I'm sure someone would be happy to tell you what function to use.

The technique you'll use, BTW, is to get the new time that has elapsed each frame, subtract that from the time that elapsed last frame and you'll have the time that has passed for each frame.

Good luck!
Without order nothing can exist - without chaos nothing can evolve.
Quote:Original post by MaulingMonkey
Quote:Original post by JohnBolton
I don't believe there is a standard C function that measures time with better-than-seconds resolution (clock() perhaps?).


Potentially. Although there's no guaranteed resolution, the value returned by clock() increases by CLOCKS_PER_SEC every second - so if CLOCKS_PER_SEC is 1000, clock() would return milliseconds. On my system it's 1,000,000.


Lol. I have a funny story here.

When I was in university, I had to do precise timing of algorithms for my undergrad thesis. I found that on the undergrad lab computers (some version of redhat linux, on sparcstations),

- clock() - intended to "determine processor time" as your manpage notes - wouldn't cut it.
- The interval timer API (getitimer/setitimer etc.) was no better.
- Ditto anything else along those lines I could think to look for.

Out of all of those things specifically intended for benchmarking CPU performance, none would give me better timing than 1/512 second.

But the one timing function in the whole damn set intended to tell humans what time of day it is yielded microsecond timing.
I've used gettimeofday for use in dead reckoning algorithms. It's useful, and the timing is very good. Don't remember what the resolution is off the top of my head, but if the above poster says microsecond, I suppose that's what it is. I don't know if it gets better than that.
gettimeofday is on *nix though, if you happen to be in the Microsoft world, one of the above posters already mentioned QPF.
--Michael Fawcett
Thanks guys, but i'm working on windows... what's the function that i should use?
See above:
Quote:Original post by JohnBolton
Anyway, if you are programming for Windows, look up QueryPerformanceCounter().


You might also want to look at this article.

Regards,
jflanglois

This topic is closed to new replies.

Advertisement