3

My colleagues normally use C or Fortran for high performance calculations (math on large arrays of data). I wonder if there is any possibility for Ruby code to be compiled/converted and come close to optimized C code in terms of performance?

There is a number of projects to compile Ruby to bytecode (Rubinius, JRuby, IronRuby, YARV?, Cardinal), and to native code (MacRuby, ..?). What are their chances to get close to C performance?

There is also almost 2-year as inactive project to convert Ruby code to C code: ruby2c.


Related questions:

Community
  • 1
  • 1
Andrei
  • 10,918
  • 12
  • 76
  • 110
  • Why do you want to do this? Don't get me wrong - I love ruby, but it would be good to know what your driver is. Have you tried some experiments or are you assuming that ruby will be slower? Depending on the nature of your data and your calculations, the performance difference may not be that great, or it might be huge. You never know until you try. – Robert Brown Jul 20 '11 at 05:13
  • Our code is quite complex, so it would be great if we could write it in Ruby instead of C. However, current Ruby implementations are up to 100 times slower than C according to [some benchmarks](http://shootout.alioth.debian.org/u64q/benchmark.php?test=all&lang=yarv&lang2=gcc). We also need to run the code on cluster, where the calcs can take a few days (C). If we could compile Ruby code to some bytecode that is just 2-3 times slower than C, then it would probably be fine. Factor of 100 is too slow for our calcs. – Andrei Jul 20 '11 at 06:19

3 Answers3

4

There's one option you didn't include: It's rather easy to extend Ruby in C, so if you have a limited set of operations that need to be fast you can write a C extension and then use that from Ruby.

http://people.apache.org/~rooneg/talks/ruby-extensions/ruby-extensions.html

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • Thanks a lot for the tip! This is certainly the most suitable solution for me. For those who are interested, there are 12 recent articles about the subject by Chris Lalancette starting at: http://clalance.blogspot.com/2011/01/writing-ruby-extensions-in-c-part-1.html – Andrei Jul 20 '11 at 13:11
4

The easiest way to reach C performance level for numerical calculations is to use specialized libraries like Ruby/GSL which are already highly optimized compiled C-code. The final performance will depend how much time your code will spend in the library.

David Unric
  • 7,421
  • 1
  • 37
  • 65
3

As already mentioned, you're going to want to use high-performance libraries with a ruby wrapper. Don't discount JRuby using java libraries for scientific computing.

Bill Dueber
  • 2,706
  • 17
  • 16