views:

223

answers:

3

I need more performance in a part of a program, coded in Ruby. In Python i could use Psyco library (that compiles the code, or a part before the execution) to improve the perfromance, but i dont know if exists something like that in Ruby.

Thanks!

+1  A: 

I'm thinking no, but you can boost performance using Ruby 1.9.

You must be careful anyway because a lot of things in the language has changed.

Carmine Paolino
Yes, in new versions of Ruby, performance is being improved a lot.Thanks, i will see the changes ;)
a0rtega
+1  A: 

earcar is right

You could update your ruby to 1.9.x, actually all versions of ruby from 1.9, comes with the YARV, that is much more faster than the old ruby interpreter, of course, this is assuming you have installed a previous version.

If you need more speed... you could write you code with c ruby extensions. Here an example..

This would be much much faster, but you have to know to program in c.

Alvaro Talavera
Yes, i thought write an extension in C, but it is not that i am searching by the moment in this program ;)
a0rtega
+2  A: 

If you know C you can optimize small parts of the code by just doping to C using rubyinline. I dont know what kind of performance improvements you can expect to see but if you are calling a few c liberys in the c bit of the code instead of ruby you should start to see some big speed ups

require 'inline'

class MyTest

def factorial(n)
   f = 1
   n.downto(2) { |x| f *= x }
   f
end

 inline do |builder|
   builder.c "
   long factorial_c(int max) {
     int i=max, result=1;
     while (i >= 2) { result *= i--; }
     return result;
  }"
 end

end

To get started: sudo gem install RubyInline

Mark