Archived

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

Deebo

Portable time retrieval

Recommended Posts

I need a portable way of getting time as precise as microseconds. There are Win32 functions for doing this but I need a portable way of doing it. I was thinking about using rdtsc, but since that is dependant on processor speed, there will be too many calculations just to see how much time has passed (or the current system time) Is this my only option? Intro Engine

Share this post


Link to post
Share on other sites
Not really. About the best you can do is use a cross-platform library that has a precise timer function (or plunder the library for the timer code and just use that). I believe SDL has one.

edit: bad bad grammar

[edited by - SiCrane on March 20, 2004 1:24:05 PM]

Share this post


Link to post
Share on other sites
I''ve never seen a portable time library aimed at multimedia or games. Boost has a time system that is based on seconds, minutes, hours, etc., but that doesn''t meet the needs of high-performance applications. It shouldn''t be too hard to begin one, and fill in other platforms when needed. You just need to abstract away exactly how you get the time, and how precise the timer is.

What I suggest is to wrap your time needs in a simple class. When you switch to a new platform you just need to change the class. The class can be based on rdtsc, timeGetTime, QueryPerformanceCounter, or whatever you want on Intel/Windows. In Linux, or on a Mac the class would use whatever functions are appropriate.

You probably want functions similar to these:
float GetCurrentTime() // in seconds
float GetFrameTime() // time between last frame and current frame
float GetFPS() // Could just use GFT(), but you probably want something smoother

As long as your entire engine uses this simple time system, switching to a new platform is a matter of filling in three functions when you make the switch... not a big deal.

Share this post


Link to post
Share on other sites
I guess so. It just seems to be more annoying than a hassle because time management is something I never really had to worry about until i started coding outside of Win32. I think I will just write a portable way of checking micro and milliseconds by just measuring clock cycles and using C runtime library functions to get the system time. Im sure other problems will come up while doing this, but i''ll handle them as they come.

Intro Engine

Share this post


Link to post
Share on other sites
SDL's time implementation is actually only good to several ms on Windows - they currently work around several QueryPerformanceCounter issues by falling back to timeGetTime.

I second the floating point GetCurrentTime interface suggestion. On *nix platforms, implement it with gettimeofday; for the Windows version, see an article I wrote on the subject. In particular, be wary of SpeedStep systems (CPUs that change their clock frequency to save power or cool down).

// edit: fix link

[edited by - Jan Wassenberg on March 20, 2004 4:59:40 PM]

Share this post


Link to post
Share on other sites
Hmmm, well i spent some time checking a few things and i found that the C runtime function, clock(), functioned the same as Win32''s GetTickCount() function. But a few times, mainly right after compiling, the sysytem would hang for a split second and my results would come different. BTW, i simply measured the cpu speed, using these functions, for my test. Will time results vary depending on the cpu state if I use these functions? Should I find another way to measure time?

Intro Engine

Share this post


Link to post
Share on other sites
Good article, just reading through the first little bit I see that I was about to go through the exact same shit you did. About the source, mind if I take a look at it to see what I can come up with. Reply to this and I will give you my e-mail address to send it to. I really hate to have to start off from square one and go throught the exact same problems you did just to end up with the same conclusion. I really like the idea of using a seperate thread to maintain an accurate frequency measurement. Do you think its possible to do that and constantly measure the frequency of the cpu, and then adjust delta''s or the current time based on varying cpu speeds? (ex. find out how many cpu clock cycles equal one microsecond at a given frequency, then determine elapsed microseconds by measuring clock cycles and adjusting for current cpu frequency)

Dont know if that comes across clear to you. Hope it does.

Intro Engine

Share this post


Link to post
Share on other sites
Glad to hear it

Here''s the timer code (GPL):
hrt.cpp
hrt.h
It won''t compile, since it depends on external system detect, RingBuffer, and x86 specific code.

Yes, you could use the calibration loop to survive CPU freq changes, but I decided not to. 2 problems: what do you do in the time you''re measuring the new frequency? And what if the freq changes often enough, that you never get a good reading? Having a timer that jumps all over the place is no good.

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
boost''s date-time library has a posix microsecond clock, and I implemented a Win32 version (though they didn''t put it in the 1.31 release...)


yeah, i had to do the same thing for a project, it was a trivial task to make it compatible as well (iirc i basicaly copied the class from the library and used it in my name space)
Gotta wonder why it didnt make it into the current release :|

Share this post


Link to post
Share on other sites
Well, I just right clicked and saved the sources.
I''m not even looking at it tonight though, I''m tired.
I will post my results tomorrow. Hopefully a few more people can benifit from this. I still can''t believe I am spending so much time on something that seemed like a 5 minute task yesterday.

Intro Engine

Share this post


Link to post
Share on other sites