bench

DESCRIPTION

Do you remeber how to use the benchmark library from the Ruby standard lib? I don’t.

Now you need not to remember, there is Bench: A DSL around the benchmark lib of the Ruby standard lib with the goal to make benchmarking as easy as possible.‘

SYNOPSIS

First an adapted example of the benchmark documentation from the pickaxe version 2 page 657

require 'bench'

string = 'Stormy Weather'
m = string.method(:length)

benchmark 'code' do
  m.call
end

benchmark 'send' do
  string.send(:length)
end

benchmark 'eval' do
  eval "string.length"
end

run 10_000

You call the run method more than once for identifying rought values.

It’s also nice to use Bench interactive with irb:

>> require 'bench'
=> true
>> benchmark 'simple' do
?>   /ll/ =~ 'hello world'
>> end
=> [#<OpenStruct name="simple", proc=#<Proc:0xb7bf7acc@(irb):2>]
>> benchmark 'freezed' do
?>   /ll/.freeze =~ 'hello world'
>> end
=> [#<OpenStruct name="simple", proc=#<Proc:0xb7bf7acc@(irb):2>, #<OpenStruct na
me="freezed", proc=#<Proc:0xb7bf26d0@(irb):5>]
>> run 1000
             user     system      total        real
simple   0.000000   0.000000   0.000000 (  0.003960)
freezed  0.010000   0.000000   0.010000 (  0.004870)
=> true
>> run 1000
             user     system      total        real
simple   0.010000   0.000000   0.010000 (  0.003969)
freezed  0.000000   0.000000   0.000000 (  0.004624)
=> true
>> # let's try more iterations
?> run 10000
             user     system      total        real
simple   0.060000   0.000000   0.060000 (  0.058049)
freezed  0.060000   0.000000   0.060000 (  0.058636)
=> true
>> run 100000
             user     system      total        real
simple   0.500000   0.000000   0.500000 (  0.502427)
freezed  0.540000   0.000000   0.540000 (  0.533421)
=> true
>> # now another benchmark sample
?> RE = /ll/
=> ll
>> benchmark 'constant' do
?>   RE =~ 'hello world'
>> end
=> [#<OpenStruct name="simple", proc=#<Proc:0xb7bf7acc@(irb):2>, #<OpenStruct name="freezed", proc=#<Proc:0xb7bf26d0@(irb):5>, #<OpenStruct name="constant", proc=#<Proc:0xb7c26250@(irb):15>]
>> run
              user     system      total        real
simple    0.000000   0.000000   0.000000 (  0.000031)
freezed   0.000000   0.000000   0.000000 (  0.000469)
constant  0.000000   0.000000   0.000000 (  0.000031)
=> true
>> run 100000
              user     system      total        real
simple    0.500000   0.000000   0.500000 (  0.507686)
freezed   0.540000   0.000000   0.540000 (  0.537840)
constant  0.550000   0.000000   0.550000 (  0.552103)
=> true
>> run 100000
              user     system      total        real
simple    0.510000   0.000000   0.510000 (  0.506002)
freezed   0.520000   0.000000   0.520000 (  0.542898)
constant  0.520000   0.020000   0.540000 (  0.552802)
=> true
>> run 100000
              user     system      total        real
simple    0.510000   0.000000   0.510000 (  0.504704)
freezed   0.530000   0.000000   0.530000 (  0.536948)
constant  0.560000   0.000000   0.560000 (  0.554470)
=> true

CREDITS

Copyright 2008 by Jan Friedrich ([email protected])

LICENSE

Ruby’s license.