accurate time function

Started by
11 comments, last by Sync Views 16 years, 2 months ago
Is there an accurate function for getting the current time/time passed since program started? I tried clock() but it seems to only update every 15 mili seconds which is to slow for what I want...
Advertisement
What programming language/compiler/operating system are you using? Are you using and third party libraries?
EDIT: removed...
c++/vcs2008/windows, The only libs i'm useing are the dirext x 9 stuff

(I take it that means there no cross platform accurate to the milisecond function for this?)
What I am currently using for a timing function for Windows is QueryPerformanceCounter and QueryPerformanceFrequency. I think you would be hard pressed to find a consistent cross-platform solution for timing unless you can find a good third party library that handles that headache for you. But since you are using DirectX are you really concerned about cross-platform?
Edmund Weese7-bit Games
If you're using DirectX then you don't have to worry about having things cross platform, since using DirectX automatically makes it windows only.

I haven't used DirectX yet since I've been going with a different approach to learning game development. However, there's a windows functions called GetTickCount() that could be what you're looking for, it gives you the number of milliseconds since the system was started.

check it out:

http://msdn2.microsoft.com/en-us/library/ms724408.aspx
You need to be careful when using GetTickCount as it will wrap around if the System has been up for 49.7 days. On the off chance that an end user is running their system in such a way, it could seriously throw off your timing if the count was to wrap back around in the middle of your program.
Edmund Weese7-bit Games
Quote:You need to be careful when using GetTickCount as it will wrap around if the System has been up for 49.7

All counters will wrap. QueryPerformanceCounter will wrap according to the processor counter frequency, although this of course is most likely to occur in a lot longer time than GetTickCount (QueryPerformanceCounter returns a 64bit value).
Anyway, i would like to point out that if you are working with the delta (between the current tick and the last tick) then wrapping of the returned tick value should not be an issue (at least in c++) as subtracting the large last tick value from the small new one will result in a negative number that will, in turn, also be wrapped (giving a normal delta value).
Also it might be interesting to note that QueryPerformanceCounter has a higher calling cost than GetTickCount
Quote:
Source : http://www.ddj.com/windows/184416651
QueryPerformanceCounter() has a high call cost on all operating systems, ranging from 49 to 2080 times that of GetTickCount().
Quote:All counters will wrap.


This is of course a true statement. And you can get the true delta if a wrap around does occur. But GetTickCount() has a lower resolution and is more likely to wrap.

Quote:(Note that highperformance_ counter has a wrap time of 100+ years. Each time the processor speed doubles, the wrapping time will halve, so when we have 1-THz machines, we will have to worry about catching the wrapping.)


Also I should point out that not all systems have a high performance counter. So you would need to deal with this situation as well. For my purposes QueryPerformanceCounter works great with a fallback of GetTickCount or a multimedia timer in the off chance that the system doesn't have a high performance counter.

Edmund Weese7-bit Games
Quote:Original post by Sync Views
(I take it that means there no cross platform accurate to the milisecond function for this?)

You're not even guaranteed millisecond accuracy with platform specific code. On some computers QueryPerformanceCounter() will actually give you the same resolution as GetTickCount() (about 55ms) and may not even be available on all systems. timeGetTime() can be as bad as ten milliseconds. I know of no timing function on Windows that is guaranteed to give you millisecond accuracy on all computers.

This topic is closed to new replies.

Advertisement