From: Joel VanderWerf Date: 2005-11-12T12:23:39+09:00 Subject: Re: Accurate Timing in ruby Daniel Sheppard wrote: > You shouldn't time your game in terms of seconds - time it in terms of > "game time" - give it a target FPS, but let it go slower. > > Each 'game logic' turn should measure the start and end time, subtract > the execution time from the target FPS and then sleep for the period of > time to make up the difference. Instead of extrapolating the FPS from > the time of the current frame, just keep a running count of frames, and > reset the count once you've entered the next second. Here's a pretty mundane implememtation of that idea, which has been useful to me in some quasi-real time work. Hope it helps somebody. http://redshift.sourceforge.net/timer/timer.rb On a 1.7GHz Pentium, running linux 2.6.10, the timer can keep fairly good time with a period of 50 microseconds, as long as you aren't doing much else. It starts slipping as you get close to 10 microsec (running as nice -n -20). I guess that gives a rough estimate of the overhead. require 'timer' count = 0 period_usec = 50.0 # microsec period = period_usec / 1_000_000 duration = 10.0 Timer.every period, duration do count += 1 end p count p duration/period -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407