eigenclass logo
MAIN  Index  Search  Changes  PageRank  Login

Over two orders of magnitude faster with YARV, yay for algorithmical optimization

Did you know that YARV features magic opcodes (not enabled by default) that can perform extensive algorithmical optimization on a number of programs?

You just have to write your program*1 with some care and enable the optimizations at compile-time:

def ack(m, n)
  if m == 0 then
    n + 1
  elsif n == 0 then
    ack(m - 1, 1)
  else
    ack(m - 1, ack(m, n - 1))
  end
end

def the_answer_to_life_the_universe_and_everything
  ack(3,7).to_s.split(//).inject(0){|s,x| s+x.to_i}.to_s + "2" 
end

t = Time.new
answer = the_answer_to_life_the_universe_and_everything
sleep(0.02) # let's take a moment to ponder about the significance of that result
puts "Got THE answer (#{answer}) in #{Time.new-t} seconds."

Here's the output under a fairly recent 1.9.0

ruby 1.9.0 (2006-01-14) [i686-linux]
Got THE answer (42) in 1.815401 seconds.

And this is with YARV, Saint Valentine special edition:

ruby 1.9.0 (2006-02-14) [i686-linux]
YARVCore 0.3.3 (rev: 419) [opts: ]
Got THE answer (42) in 0.027716 seconds.

Yes, that's two orders of magnitude, just by rearranging the code a bit!

It gets even better: there's no bound to the amount of time we can save with YARV!!!

Generalization


Deeply recursive methods aren't the only ones that can benefit from such an optimization. The interested reader can refer to YARV's sources (in particular, grep compile.c for bitblt and answer, and look at the opcode definitions in vm.inc). The compile-time CFLAGS option necessary to enable the optimized opcodes is left as an exercise.

You can drop a comment with your success stories

wink.gif

Unrelated comment - Kevin (2006-02-19 (Sun) 11:57:09)

This is completely unrelated to the post (which looks totally cool!) but...

Could you put an 'About' link at the top of the page, just a couple sentences putting the site in some context. I hit this site and I'm like, 'pretty ruby code everywhere, but what IS this magical place?'.

Great site though, tons of cool posts. You've got me psyched about YARV, that's for sure.


mfp 2006-02-25 (Sat) 05:58:15

ACK, will add a desc somewhere. Thanks for the kudos ^_^;

Last modified:2006/02/16 06:17:41
Keyword(s):[ruby] [blog] [yarv] [joke]
References:

*1 not all will benefit from these optimizations, though ;)