Jump to content
  • Advertisement
Sign in to follow this  
Helderash

Accurate Timing

This topic is 3984 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

Hey folks! I'm working on a Physics related project, where frame times may vary greatly depending on the computations being performed at the time, and am in need of an accurate way of measuring the frame time for movement purposes. I'm currently using the following:
double frameTime;

start = clock();
//FRAME
end = clock();
frameTime = ((double) (end - start)) / CLOCKS_PER_SEC;
In the majority of cases however the result is zero, so I'm guessing this method isn't accurate enough during high frame rates. If anyone knows of a more accurate method I would very much appreciate it! Thankyou for the help!

Share this post


Link to post
Share on other sites
Advertisement
What operating system and/or other libraries are you using? Some platforms supply better timers like Window's QueryPerformanceCounter().

Share this post


Link to post
Share on other sites
I'm currently running on Windows 2000 (SP4), and using the Irrlicht 3D v1.3 graphics library.

I'll check to see whether QueryPerformanceCounter() is available on Windows 2000. *Fingers crossed*.

Share this post


Link to post
Share on other sites
How about:

__declspec(naked)
unsigned __int64 __cdecl GetCycles()
{
__asm
{
rdtsc
ret
}
}


example useage:
int main()
{
__int64 t1 = GetTime();
for( int i = 0; i < 10; i++ )
{
DoSomething();
}
__int64 t2 = GetTime();
__int64 result = t2 - t1;
return 0;
}



edit: can someone tell me the code tags? :)

[Edited by - Dolf on July 23, 2007 2:20:45 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Dolf
How about:
*** Source Snippet Removed ***

edit: can someone tell me the code tags? :)


I am pretty sure that is a bad idea, at least on windows. Something to do with the built in clock not always being accurate, especially on multithreaded computers. Use the QueryPerformanceCounter API, which works around several limitations and is very accurate for most timing needs.

Share this post


Link to post
Share on other sites
Thankyou all for your help, I really appreciate it! I'll investigate the QueryPerformanceCounter API today and hopefully get things working correctly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
Quote:
Original post by Dolf
How about:
*** Source Snippet Removed ***

edit: can someone tell me the code tags? :)


I am pretty sure that is a bad idea, at least on windows. Something to do with the built in clock not always being accurate, especially on multithreaded computers. Use the QueryPerformanceCounter API, which works around several limitations and is very accurate for most timing needs.


Can you show me a reference on that? I've been using this method because it was recommended from Intel's documentation.

Share this post


Link to post
Share on other sites
Quote:
Original post by David Neubelt
Quote:
Original post by Mike.Popoloski
Quote:
Original post by Dolf
How about:
*** Source Snippet Removed ***

edit: can someone tell me the code tags? :)


I am pretty sure that is a bad idea, at least on windows. Something to do with the built in clock not always being accurate, especially on multithreaded computers. Use the QueryPerformanceCounter API, which works around several limitations and is very accurate for most timing needs.


Can you show me a reference on that? I've been using this method because it was recommended from Intel's documentation.


I just remember it from the back of my mind. I never forget these little tidbit things. One sec, let me take a look around at Google...

Ah, well the first result was Wikipedia. Not the best reference around, but I am too lazy to look for more.

The article confirms what I remember. Basically, it was really good for Intel x86 platforms, but you can run into issues because:

1) It only works on Intel x86 platforms. Any others will provide you with a ton of headaches.

2) It gets thrown off by multi core processors.

3) It is no longer garaunteed to work on newer CPUs anyway, because of their power saving features.

4) You can't use it in managed programs because it requires assembly, whereas QueryPerformanceCounter can be PInvoked.

Share this post


Link to post
Share on other sites
RDTSC will probably work on single core intel processors. The operating system does a lot of thread shifting among processors, so if a thread shifts, the value will be completely off on multicore processors. Power management might come into play, IIRC, because the frequency of the processor might change in LP mode.

EDIT: Beaten to it, didn't see above post.

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!