Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High performance calculations with Ruby?

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:

  • Is it possible to compile Ruby to byte code as with Python?
  • Can Ruby, PHP, or Perl create a pre-compiled file for the code like Python?
like image 976
Andrei Avatar asked Feb 06 '26 00:02

Andrei


2 Answers

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

like image 66
Michael Kohl Avatar answered Feb 09 '26 07:02

Michael Kohl


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.

like image 37
David Unric Avatar answered Feb 09 '26 09:02

David Unric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!