Accurate Timing

Started by
10 comments, last by Mike.Popoloski 16 years, 9 months ago
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!
Advertisement
What operating system and/or other libraries are you using? Some platforms supply better timers like Window's QueryPerformanceCounter().
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*.
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]
Quote:edit: can someone tell me the code tags? :)

source and /source, both inside []'s
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.
Mike Popoloski | Journal | SlimDX
Thankyou all for your help, I really appreciate it! I'll investigate the QueryPerformanceCounter API today and hopefully get things working correctly.
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.
Graphics Programmer - Ready At Dawn Studios
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.
Mike Popoloski | Journal | SlimDX
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.

This topic is closed to new replies.

Advertisement