benchmark

require 'benchmark'

total = (ENV['TOTAL'] || 1_000).to_i

strings = Dir["spec/**/*_spec.rb"]
numbers = Array.new(strings.size).fill { |i| strings[i].size }

Benchmark.bmbm do |x|
  x.report("Fixnum#<=>") do
    times = 0
    while times < total
      size = numbers.size
      i = j = 0
      while i < size
        while j < size
          numbers[i] <=> numbers[j]
          j += 1
        end
        i += 1
      end
      times += 1
    end
  end

  x.report("String#<=>") do
    times = 0
    while times < total
      size = numbers.size
      i = j = 0
      while i < size
        while j < size
          strings[i] <=> strings[j]
          j += 1
        end
        i += 1
      end
      times += 1
    end
  end
end

before

angelo:rubinius brian$ ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]

angelo:rubinius brian$ TOTAL=1_000 ruby spaceship.rb 
Rehearsal ----------------------------------------------
Fixnum#<=>   2.140000   0.000000   2.140000 (  2.141557)
String#<=>   2.240000   0.000000   2.240000 (  2.252411)
------------------------------------- total: 4.380000sec

                 user     system      total        real
Fixnum#<=>   2.150000   0.010000   2.160000 (  2.160930)
String#<=>   2.260000   0.000000   2.260000 (  2.255196)


angelo:rubinius brian$ shotgun/rubinius -v
rubinius 0.8.0 (ruby 1.8.6 compatible) (681156e0b) (02/05/2008) [i686-apple-darwin9.1.0]

angelo:rubinius brian$ TOTAL=1_000 shotgun/rubinius spaceship.rb 
Rehearsal ----------------------------------------------
Fixnum#<=>   0.576426   0.000000   0.576426 (  0.576411)
String#<=>  12.772771   0.000000  12.772771 ( 12.772771)
------------------------------------ total: 13.349197sec

                 user     system      total        real
Fixnum#<=>   0.581613   0.000000   0.581613 (  0.581600)
String#<=>  12.761486   0.000000  12.761486 ( 12.761486)

after while replaced Integer#times

angelo:rubinius brian$ TOTAL=1_000 shotgun/rubinius spaceship.rb 
Rehearsal ----------------------------------------------
Fixnum#<=>   0.579724   0.000000   0.579724 (  0.579695)
String#<=>   5.834347   0.000000   5.834347 (  5.834344)
------------------------------------- total: 6.414071sec

                 user     system      total        real
Fixnum#<=>   0.582978   0.000000   0.582978 (  0.582972)
String#<=>   6.122626   0.000000   6.122626 (  6.122642)

after ByteArray#compare_bytes fixed to take lengths

angelo:rubinius brian$ rm spaceship.rbc; TOTAL=1_000 shotgun/rubinius spaceship.rb 
Rehearsal ----------------------------------------------
Fixnum#<=>   0.613617   0.000000   0.613617 (  0.613589)
String#<=>   2.417588   0.000000   2.417588 (  2.417580)
------------------------------------- total: 3.031205sec

                 user     system      total        real
Fixnum#<=>   0.609608   0.000000   0.609608 (  0.609608)
String#<=>   2.440267   0.000000   2.440267 (  2.440282)