Skip to content

Instantly share code, notes, and snippets.

@rdp
Created March 15, 2010 14:31
Show Gist options
  • Select an option

  • Save rdp/332879 to your computer and use it in GitHub Desktop.

Select an option

Save rdp/332879 to your computer and use it in GitHub Desktop.
require 'benchmark'
def yields
yield
end
def named_block &block
block.call
end
class A
def go
end
def get_proc
@proc ||= proc {}
end
end
3.times {
Benchmark.bm do |x|
n = 100000
x.report("straight yield ") { n.times { yields {}}}
x.report("named block ") { n.times { named_block {}}}
a = A.new
x.report("cached proc ") { n.times { named_block(&a.get_proc) }}
end
}
jruby --server
straight yield 0.047000 0.000000 0.047000 ( 0.047000) * slower
named block 0.094000 0.000000 0.094000 ( 0.094000)
cached proc 0.093000 0.000000 0.093000 ( 0.093000) * slower
jruby --server --fast
straight yield 0.031000 0.000000 0.031000 ( 0.031000)
named block 0.094000 0.000000 0.094000 ( 0.094000)
cached proc 0.078000 0.000000 0.078000 ( 0.078000)
1.9.1
straight yield 0.031000 0.000000 0.031000 ( 0.031250)
named block 0.672000 0.015000 0.687000 ( 0.687500)
cached proc 0.078000 0.000000 0.078000 ( 0.078125)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment