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)ifm==0thenn+1elsifn==0thenack(m-1,1)elseack(m-1,ack(m,n-1))endenddef the_answer_to_life_the_universe_and_everythingack(3,7).to_s.split(//).inject(0){|s,x|s+x.to_i}.to_s+"2"endt=Time.newanswer=the_answer_to_life_the_universe_and_everythingsleep(0.02)# let's take a moment to ponder about the significance of that resultputs"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!!!