OpenGL 3D gears demo for Ruby

Hi there,

I'm a recent convert from Python, PHP et al., and aside from more
serious web-related development with Rails, have been playing a bit
with Yoshi's OpenGL/GLUT bindings. I was interested to see how
ruby-opengl performance would compare with C/C++, so I wrote a Ruby
version of the (in)famous spinning 3D gear wheels demo program, which
is often used as a simple benchmark for OpenGL.

(Kudos to Yoshi for the OpenGL bindings: translating from the original
C code was straightforward and only took an hour or so to finish.)

The results were a bit surprising, in a good way. On my Linux computer
with ATI Radeon 9700, the compiled version of the original gears.c
gives an average 1271 FPS, whereas the gears.rb version achieves an
average of 1203 FPS. (Both programs were run for a couple of minutes
and results averaged.) That's only a 6% gap in performance.

Granted this is a fairly simple OpenGL app, but it's certainly easy to
imagine how productive programmers of open-source games, simulations
etc. could be writing Ruby instead of C/C++ as is the norm.

I've attached the Ruby file gears.rb. It only requires the ruby-opengl
extension to run. The code was translated from rev 1.8 of the
GLUT-based gears.c (not the same as glxgears!) included in the Mesa 3D
software package, keeping the structure close to the original, but
wrapping it in a Ruby class. I also added some simple code to do mouse
rotation. If anyone wishes to compare performance with the original
version, it can downloaded from:
http://cvs.freedesktop.org/mesa/Mesa/progs/demos/gears.c

Arto Bendiken

gears.rb (8.91 KB)

su, 2005-05-01 kello 09:02, Arto Bendiken kirjoitti:

Hi there,

The results were a bit surprising, in a good way. On my Linux computer
with ATI Radeon 9700, the compiled version of the original gears.c
gives an average 1271 FPS, whereas the gears.rb version achieves an
average of 1203 FPS. (Both programs were run for a couple of minutes
and results averaged.) That's only a 6% gap in performance.

Hello,
The reason the Ruby version runs fast is because the gears-program uses
display lists and hence the draw function is pretty much limited just by
the speed of OpenGL's display list drawing efficiency.

I did a couple of experiments comparing drawing the gears with and
without display lists and got the following results:

C, using display lists: 3321FPS
Ruby, using display lists: 3033FPS

C, without display lists: 2417FPS
Ruby, without display lists: 90FPS

The 27x speed difference sounds about right..

The gear function could probably be optimized by minimizing the amount
of Ruby math there and doing as much as possible with GL.Translate and
GL.Rotate, since the GPU does those (and fast!).

It's definitely possible to use Ruby for writing GL apps, though good
performance requires doing things in a way that uses GL's
performance-helping things as much as possible (HW T&L, vertex arrays,
display lists, or even writing inner loops and math-intensive parts in
C).

Anxiously waiting for the fast VM(s), :slight_smile:
Ilmari