Archived

This topic is now archived and is closed to further replies.

Portable High Performance Timer

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

Does anyone know of a portable alternative to Win32''s Performance Counter for high frequency timing? Otherwise, does anyone know of any good resources that would help me create such a timer on platforms other than windows? Thanks.

Share this post


Link to post
Share on other sites
If gettimeofday() is portable enough for you, perhaps use that.

What kinds of thing did you have in mind? AFAIK gettimeofday is available on a lot of C libraries, but I don''t know about how similar the structure is. Of course its resolution will be OS-dependent (probably 1/18s on DOS )

Mark

Share this post


Link to post
Share on other sites
Well, on PCs you have access to the rdtsc command (through inline assembler) which pushes the current clock cycle count into registers, which is as high resoluton as you can get. Any non-PC hardware (or pre-Pentium PCs) won''t have this command. The best way to code a portable timer is through use of #define''s and #ifdef for conditional compilation depending on the platform. I''m afraid on different platforms there is no guarantee that a particular timing method will be available. You''ll just need to find the best timer for each platform and add them to your code with the necessary #ifdef / #else / #endif around them.

Share this post


Link to post
Share on other sites
Not completely sure that on every Pentium-compatible platform, RDTSC is guaranteed to continue to be incremented at the same rate over time.

For instance, on mobile (i.e. laptop) CPUs, which have clock frequency switching, is there a possibility that RDTSC will vary with the clock speed?

I don''t think the OS implementation of gettimeofday / QueryPerformanceCounter relies on RDTSC, instead it asks a timer chip somewhere what time it is?

Mark

Share this post


Link to post
Share on other sites
clock() does something different from RDTSC.

From the man page:

quote:

The clock() function returns an approximation of processor time used by the program.


Note "by the program". So clock() isn''t actually looking at a real-time or "wall" clock, it''s looking at the amount of CPU time the program has used. The Linux man page for clock() does not specify how it handles threads, but I''m not sure it would give you the result you expect.

----

I still recommend that you manually code an implementation of your high resolution timer, changing it if necessary for other platforms.

Are there any credible gaming platforms other than win32 which don''t have a useable getttimeofday()? If so, you will have to code another implementation for them too (NB I know nothing about the availability of this in such things as PS2 C library)

Mark

Share this post


Link to post
Share on other sites
quote:
Original post by markr
For instance, on mobile (i.e. laptop) CPUs, which have clock frequency switching, is there a possibility that RDTSC will vary with the clock speed?


Yes indeed. This happens with my P4M, even when it''s plugged in. The only way around it is to switch off all power saving features in the BIOS which isn''t really an option. I assume that it won''t work with processors like the Crusoe either.

Fortunately no commercial games I''ve tried use this method, but B5:IFH (http://ifh.firstones.com/) does and everything runs 3-4 times faster than it should.

Share this post


Link to post
Share on other sites