Jump to content
  • Advertisement
Sign in to follow this  
Sync Views

accurate time function

This topic is 3784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
Advertisement
What programming language/compiler/operating system are you using? Are you using and third party libraries?

Share this post


Link to post
Share on other sites
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?)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!