On Feb 12, 2007, at 12:26 PM, Robert Dober wrote:

> 521/21 > cat bench.rb
>  require 'benchmark'
>
>    array = (1..100000).map { rand * (ARGV.first||1_000_000).to_f }
>
> def phrogz quiz
>  i,f=quiz.to_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?
> ('.'+f) : '')
> end
> def james quiz
>  quiz.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,"\\1,").reverse
> end
>
>
>    Benchmark.bmbm do |x|
>      x.report("Phrogz") {array.map{ |e| phrogz(e) }}
>      x.report("James")  {array.map{ |e| james(e) }}
>    end

How does this code work?  You pass arguments to methods we don't  
see.  The ones we do see don't even accept arguments.

The results look right though:

#!/usr/bin/env ruby -w

require "benchmark"

def phrogz(num)
   i,f=num.to_s.split('.'); i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (f ?  
('.'+f) : '')
end

def james(num)
   num.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,"\\1,").reverse
end

TESTS = Array.new(100_000) { rand(1_000_000) + 1.to_f / (rand(1_000)  
+ 1) }
Benchmark.bmbm do |results|
   results.report("Phrogz:") { TESTS.each { |n| phrogz(n) } }
   results.report("James:") { TESTS.each { |n| james(n) } }
end
# >> Rehearsal -------------------------------------------
# >> Phrogz:   1.690000   0.010000   1.700000 (  1.700985)
# >> James:    1.550000   0.010000   1.560000 (  1.557882)
# >> ---------------------------------- total: 3.260000sec
# >>
# >>               user     system      total        real
# >> Phrogz:   1.690000   0.010000   1.700000 (  1.703213)
# >> James:    1.520000   0.000000   1.520000 (  1.528621)

__END__

James Edward Gray II