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("loop") do
    times = 0
    while times < total
      size = numbers.size
      i = j = 0
      while i < size
        while j < size
          j += 1
        end
        i += 1
      end
      times += 1
    end
  end

  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

ruby


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

angelo:rubinius brian$ TOTAL=1_000 ruby string.rb 
Rehearsal ---------------------------------------------
loop        1.510000   0.000000   1.510000 (  1.540249)
Fixnum#==   2.240000   0.000000   2.240000 (  2.267833)
String#==   2.250000   0.000000   2.250000 (  2.274516)
------------------------------------ total: 6.000000sec

                user     system      total        real
loop        1.520000   0.010000   1.530000 (  1.553980)
Fixnum#==   2.240000   0.000000   2.240000 (  2.278529)
String#==   2.280000   0.010000   2.290000 (  2.336008)

with :string_equal primitive


			
angelo:rubinius brian$ rm string.rbc; TOTAL=1_000 shotgun/rubinius string.rb 
Rehearsal ---------------------------------------------
loop        0.290627   0.000000   0.290627 (  0.290615)
Fixnum#==   0.544032   0.000000   0.544032 (  0.544017)
String#==   0.614593   0.000000   0.614593 (  0.614575)
------------------------------------ total: 1.449252sec

                user     system      total        real
loop        0.286488   0.000000   0.286488 (  0.286496)
Fixnum#==   0.555419   0.000000   0.555419 (  0.555413)
String#==   0.612422   0.000000   0.612422 (  0.612424)

with ByteArray#compare_bytes


			
angelo:rubinius brian$ rm string.rbc; TOTAL=1_000 shotgun/rubinius string.rb 
Rehearsal ----------------------------------------------
Fixnum#==    0.741157   0.000000   0.741157 (  0.741143)
String#==    2.207268   0.000000   2.207268 (  2.207269)
------------------------------------- total: 2.948425sec

                 user     system      total        real
Fixnum#==    0.555471   0.000000   0.555471 (  0.555461)
String#==    2.160827   0.000000   2.160827 (  2.160836)