Accurate Timing in ruby

Believe it or not, I'm attempting to make an action-driven game with ruby. Is there a way to get the time in milliseconds since the program started, or something similar? I've tried using Time.now.to_f, but all too often that value will be the same in two consecutive update calls, causing jumps in motion and "infinite" fps readings.

Also, will extending calculation-heavy classes to C up the performance enough to make a fairly sophisticated game run? I'm talking like maybe a NES sports game as far as complexity goes. For example, a vector class has about 15-20 methods that all do a few simple computations. Would C significantly speed that up or would the fact that ruby still has to make the calls bog it back down?

···

---------------------------------
Yahoo! FareChase - Search multiple travel sites in one click.

Believe it or not, I'm attempting to make an action-driven game with ruby. Is there a way to get the time in milliseconds since the program started, or something similar? I've tried using Time.now.to_f, but all too often that value will be the same in two consecutive update calls, causing jumps in motion and "infinite" fps readings.

Would Time.now.usec help? Returns the time in microseconds.

Also, will extending calculation-heavy classes to C up the performance enough to make a fairly sophisticated game run? I'm talking like maybe a NES sports game as far as complexity goes. For example, a vector class has about 15-20 methods that all do a few simple computations. Would C significantly speed that up or would the fact that ruby still has to make the calls bog it back down?

Don't use C unless you're sure that Ruby isn't fast enough, and be
sure you know where the code is not fast enough. And then optimize
the slow code (first by using a better algorithm, and then by using a
better algorithm, and then use C).

···

On 11/10/05, Eric Hofreiter <erichof425@yahoo.com> wrote:

Believe it or not, I'm attempting to make an action-driven game with ruby
  Is there a way to get the time in milliseconds since the program start
ed, or something similar? I've tried using Time.now.to_f, but all too of
ten that value will be the same in two consecutive update calls, causing
jumps in motion and "infinite" fps readings.

I typically & rather consistently get:

  irb(main):003:0> Time.now - Time.now
  => -5.0e-06

meaning Ruby evaluates from right to left and my clock is running
nice and smooth (on microsecond level)

I fear what you see is not Ruby's fault, but your hardware/OS clock.
1) your hardware is so fast that the consecutive calls have no
   significant distinguishing bits left (I have 2 or 3 bits left on 700
   MHz; you may need nanoseconds instead of microseconds)
2) your OS is not updating its system clock often enough

Even though windows has nasty habits when updating time to a central
server, I do not think it suffers from 2)

You don't round to milliseconds before you compute the fps, do you?
Then you would get zero whenever you're near 1000 fps (which --depending
on the action-- is quite possible on today's hardware; even my puny CPU
and reasonable graphics card have 1200fps on the glxgears test)

Hth,
Kero.

+--- Kero ---------------------------------- kero@chello@nl ---+

The last good thing written in C++ was the Pachelbel Canon |
Jerry Olson |

+--- M38c ------------ http://members.chello.nl/k.vangelder ---+

Kero wrote:

Believe it or not, I'm attempting to make an action-driven game with ruby
  Is there a way to get the time in milliseconds since the program start
ed, or something similar? I've tried using Time.now.to_f, but all too of
ten that value will be the same in two consecutive update calls, causing
jumps in motion and "infinite" fps readings.

[...]

I fear what you see is not Ruby's fault, but your hardware/OS clock.
1) your hardware is so fast that the consecutive calls have no
   significant distinguishing bits left (I have 2 or 3 bits left on 700
   MHz; you may need nanoseconds instead of microseconds)
2) your OS is not updating its system clock often enough

Even though windows has nasty habits when updating time to a central
server, I do not think it suffers from 2)

In my experience it does:

irb(main):006:0> a=Time.now;while Time.now==a;end;Time.now-a
=> 0.01

ie. Time.now only gives you the time in centiseconds.

I would recommend the initial poster to use one of the SDL bindings, if possible, which include methods to query the time much more accurately.

Dennis

In my experience it does:

irb(main):006:0> a=Time.now;while Time.now==a;end;Time.now-a
=> 0.01

ie. Time.now only gives you the time in centiseconds.

That's bad.

On linux (700 MHz Duron) I get:

  irb(main):036:0> a=Time.now;while Time.now==a;end;Time.now-a
  => 2.0e-05

And on my iPAQ w/ linux (200 MHz strongarm) I get:

  irb(main):001:0> a=Time.now;while Time.now==a;end;Time.now-a
  => 0.000271

(still with usec accuracy)

So my PDA is more accurate than your machine (what OS is that?)

Bye,
Kero.

Kero wrote:

In my experience it does:

irb(main):006:0> a=Time.now;while Time.now==a;end;Time.now-a
=> 0.01

ie. Time.now only gives you the time in centiseconds.

That's bad.

On linux (700 MHz Duron) I get:

  irb(main):036:0> a=Time.now;while Time.now==a;end;Time.now-a
  => 2.0e-05

And on my iPAQ w/ linux (200 MHz strongarm) I get:

  irb(main):001:0> a=Time.now;while Time.now==a;end;Time.now-a
  => 0.000271

(still with usec accuracy)

So my PDA is more accurate than your machine (what OS is that?)

Windows XP.
I think I got better accuracy on my iBook as well, so it's probably a
limitation of the OS functions ruby uses to retreive the time on Windows.

Dennis

Linux on a pathetic old box:
$ irb
irb(main):001:0> a=Time.now;while Time.now==a;end;Time.now-a
=> 4.4e-05
irb(main):002:0>
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 5
model name : Pentium II (Deschutes)
stepping : 1
cpu MHz : 350.802
cache size : 512 KB

Regards,
Jason
http://blog.casey-sweat.us/

···

On 11/11/05, Kero <kero@chello.single-dot.nl> wrote:

> In my experience it does:
>
> irb(main):006:0> a=Time.now;while Time.now==a;end;Time.now-a
> => 0.01
>
> ie. Time.now only gives you the time in centiseconds.

That's bad.

On linux (700 MHz Duron) I get:

  irb(main):036:0> a=Time.now;while Time.now==a;end;Time.now-a
  => 2.0e-05

And on my iPAQ w/ linux (200 MHz strongarm) I get:

  irb(main):001:0> a=Time.now;while Time.now==a;end;Time.now-a
  => 0.000271

(still with usec accuracy)

So my PDA is more accurate than your machine (what OS is that?)

> So my PDA is more accurate than your machine (what OS is that?)

Windows XP.
I think I got better accuracy on my iBook as well, so it's probably a
limitation of the OS functions ruby uses to retreive the time on Windows.

Mmm, I think your right ... my XP box reports:

irb(main):001:0> a=Time.now;while Time.now==a;end;Time.now-a
=> 0.016

for a Athlon 2800.

Dennis Ranke wrote:

Kero wrote:

So my PDA is more accurate than your machine (what OS is that?)

Windows XP.
I think I got better accuracy on my iBook as well, so it's probably a
limitation of the OS functions ruby uses to retreive the time on Windows.

True. There has been an MSDN magazine article about this (beware, I
have only skimmed it):

  Microsoft Learn: Build skills that open doors in your career

In short, Windows' clock is as accurate as 10 or 15 milliseconds,
depending on the hardware. The article also seems to show how to
implement a more precise timer in C++ - it should also work in Ruby,
but you might need to use Win32API.

Good luck,
Markus

Silly Windows.
I haven't looked at the source but it sounds like it might be using
::GetTickCount(), if thats the case it could probably be refactored to use
::QueryPerformanceCounter() which is a significantly higher resolution
timer. Of course, then questions of 9x compatibility come up.
The idea of using the SDL wrapper for making a game is probably a good one
though.
-Harold

···

On 11/11/05, Robert McGovern <robert.mcgovern@gmail.com> wrote:

> > So my PDA is more accurate than your machine (what OS is that?)
>
> Windows XP.
> I think I got better accuracy on my iBook as well, so it's probably a
> limitation of the OS functions ruby uses to retreive the time on
Windows.

Mmm, I think your right ... my XP box reports:

irb(main):001:0> a=Time.now;while Time.now==a;end;Time.now-a
=> 0.016

for a Athlon 2800.