Sign in to follow this  

accurate time function

This topic is 3596 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
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
If that is the case how can I ensure that something loops 100 times a second? Most of the ways ive tried have been off by up to 15ms which makes it far less than 100 times a second :(

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
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.


Why isn't there a half-decent standard for such a seemingly simple task as reporting an accurate time, anyway?

(Note that Linux has its share of oddities, too. On the systems I've used, all the functions documented as being used for measuring precise times, benchmarking performance etc. give either 1/100s or 1/512s accuracy, while the one function documented - and named - as being used for telling people the time of day gives microsecond accuracy. Go figure.)

Share this post


Link to post
Share on other sites
I'm still not sure how to get around this problem... I tried the HighProformanceCounter but I get stuff like "error C2676: binary '-' : 'LARGE_INTEGER' does not define this operator or a conversion to a type acceptable to the predefined operator" with almost everything I try to do with the returned value and even if I get it working not all systems support it...

There must be some relibable way to do stuff like loop something x times a second without each loop being up to 16ms out(which is quite alot when I want it to loop once every 10ms...) or to find exactly how many miliseconds something took to execute....

Share this post


Link to post
Share on other sites

This topic is 3596 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this