Ruby Shootout: Fasta

Lately, I’ve been looking at the shootout Ruby benchmarks. I’d gotten into a habit of checking them every few months, rooting for my favorite languages. Not really understanding why some didn’t have the greatest showing on there. When the people running it upgraded their hardware, it seems as though Ruby fell off the list. While Ruby is a slow language, it deserves its spot (even if it is the bottom). I looked at the submissions and saw a post saying if more Ruby benchmarks were updated it would be included again.

I’ve since updated 3 of the benchmarks; I sped up reverse-compliment to be 2x faster than the previous. I’m not a Ruby expert by any means, but I was able to get a speedup. I don’t know if the other languages are in the same state, but they may be…

Anyway, the Fasta benchmark is the one that I most recently updated. It took me a while to get a decent speedup. It turned out the Array#find is slower than Array#each with a break statement. Making this switch is what got the biggest speedup out of the program.

I wrote a benchmark showing the difference in runtime:

require 'benchmark'
 
N = 300
table = (0..300).to_a
Benchmark.bmbm(8) do |x|
  x.report("Array.find") {
    N.times do
      table.each do |find_num|
	table.find do |num|
	  num > find_num
	end
      end
    end
  }
 
  x.report("Array.each") {
    N.times do
      table.each do |find_num|
	output = nil
	table.each do |num|
	  if num > find_num then
	    output = num
	    break
	  end
	end
      end
    end
  }
end

The results:

Ruby 1.8.6       user     system      total        real
Array.find  12.120000   0.020000  12.140000 ( 14.647705)
Array.each   8.150000   0.030000   8.180000 (  9.734463)

JRuby 1.1.4      user     system      total        real
Array.find   6.738000   0.000000   6.738000 (  6.738407)
Array.each   5.365000   0.000000   5.365000 (  5.364320)

Now, I’m running this on an admittedly dated machine. Again, I’m nowhere near a ruby expert, so if someone sees a way to make the benchmark more fair let me know, and I’ll update it. I can’t say anything about how this would run in YARV when that is released.

Here is a link to the Ruby Fasta benchmark on the Language Shootout:

Ruby Shootout: Fasta

Posted on November 2, 2008 at 8:39 am by Joe · Permalink · 3 Comments
In: Ruby · Tagged with: , ,