[C]Does anyone know a good timing library?<solved>
Moderator: MaxCoderz Staff
[C]Does anyone know a good timing library?<solved>
I need a fairly good timing library that can manipulate milliseconds and is cross-platform because timing.h just isn't doing the trick. So can anyone help me with a suggestion?
Last edited by Halifax on Thu 11 Oct, 2007 2:17 am, edited 1 time in total.
- benryves
- Maxcoderz Staff
- Posts: 3089
- Joined: Thu 16 Dec, 2004 10:06 pm
- Location: Croydon, England
- Contact:
What are you looking for? A date/time library (for the manipulation bit), or a timer library? Do you want to time durations or do you want a timer to execute code at fixed intervals?
Under Windows the only reliable millisecond-accurate timing is part of the multimedia API, and even there it is recommended that you don't use any API calls apart from the MIDI ones.
I'm guessing that high-precision timers vary wildly by OS. If you could narrow down your request you could probably get away with writing your own methods that use OS-dependant functionality without too much hassle...
Under Windows the only reliable millisecond-accurate timing is part of the multimedia API, and even there it is recommended that you don't use any API calls apart from the MIDI ones.
I'm guessing that high-precision timers vary wildly by OS. If you could narrow down your request you could probably get away with writing your own methods that use OS-dependant functionality without too much hassle...
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
Odd indeed..
But can't you make a thread sleep for a number of milliseconds on all multy-threaded platforms? If so, you could just run a thread that loops forever and sleeps 1 millisecond each iteration and report a tick each time it finished sleeping, right? like so?:
Of course I wouldn't know, I've never done anything of the sort..
But can't you make a thread sleep for a number of milliseconds on all multy-threaded platforms? If so, you could just run a thread that loops forever and sleeps 1 millisecond each iteration and report a tick each time it finished sleeping, right? like so?:
Code: Select all
while (true)
{
sleep(1);
reportTick();
}
- benryves
- Maxcoderz Staff
- Posts: 3089
- Joined: Thu 16 Dec, 2004 10:06 pm
- Location: Croydon, England
- Contact:
Sleeping a thread is only guaranteed to give time to the rest of the system for at least the time specified, and the time is typically much much longer (tens to hundreds of ms). You might have more luck bumping up the thread's priority to realtime, but that's fairly inelegant.
Getting code to execute at a fixed, accurate time step with ms-granularity isn't really practical as far as running multiple applications at once go. If this is for a game I'd suggest locking to the monitor's vertical blank, or to not worry about it at all and just let your code run as fast as possible, using QueryPerformanceCounter (Win32 API) or DateTime (.NET) to calculate the time difference between updates and using that to scale logic speed.
Getting code to execute at a fixed, accurate time step with ms-granularity isn't really practical as far as running multiple applications at once go. If this is for a game I'd suggest locking to the monitor's vertical blank, or to not worry about it at all and just let your code run as fast as possible, using QueryPerformanceCounter (Win32 API) or DateTime (.NET) to calculate the time difference between updates and using that to scale logic speed.
- Jim e
- Calc King
- Posts: 2457
- Joined: Sun 26 Dec, 2004 5:27 am
- Location: SXIOPO = Infinite lives for both players
- Contact:
QueryPerformanceCounter is a bad idea. It varies wildly between computers. Intuition says it should be the cpu clock, but that does not seem to be the case in all instances. Trust me, we tried it in wabbitemu, and it caused more grief.
Here, read from one of the few programming blogs I respect.
http://www.virtualdub.org/blog/pivot/entry.php?id=106
Sleep also fails just like Ben says, it waits at least that amount of time.
Simple method of synchronizing would be to keep track of the virtual time in your game, and the real time. If you get ahead of the real time, wait until it catches up(and do any work thats not timing specific). If you fall behind real time then you'll likely need to skip frames and execute as fast as possible.
Now as far as cross platform goes, no idea. There's a way, I just don't know.
Here, read from one of the few programming blogs I respect.
http://www.virtualdub.org/blog/pivot/entry.php?id=106
Sleep also fails just like Ben says, it waits at least that amount of time.
Simple method of synchronizing would be to keep track of the virtual time in your game, and the real time. If you get ahead of the real time, wait until it catches up(and do any work thats not timing specific). If you fall behind real time then you'll likely need to skip frames and execute as fast as possible.
Now as far as cross platform goes, no idea. There's a way, I just don't know.
- benryves
- Maxcoderz Staff
- Posts: 3089
- Joined: Thu 16 Dec, 2004 10:06 pm
- Location: Croydon, England
- Contact:
Hm, curious. Did the problems persist if you forced the thread affinity to a particular core and/or disable variable CPU clock rate?Jim e wrote:QueryPerformanceCounter is a bad idea. It varies wildly between computers. Intuition says it should be the cpu clock, but that does not seem to be the case in all instances. Trust me, we tried it in wabbitemu, and it caused more grief.
- Jim e
- Calc King
- Posts: 2457
- Joined: Sun 26 Dec, 2004 5:27 am
- Location: SXIOPO = Infinite lives for both players
- Contact:
For me it worked fine actually, I'm on an intel. Spencer was on an AMD, it was his clocks speed divided by some number that made it utterly useless. But after reading up on it, we gave up. I heard nothing but woes about it.benryves wrote:Hm, curious. Did the problems persist if you forced the thread affinity to a particular core and/or disable variable CPU clock rate?Jim e wrote:QueryPerformanceCounter is a bad idea. It varies wildly between computers. Intuition says it should be the cpu clock, but that does not seem to be the case in all instances. Trust me, we tried it in wabbitemu, and it caused more grief.