Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'benchmark'
- def with_call(object, &block)
- block.call(object)
- end
- def with_proc(object)
- Proc.new.call(object)
- end
- def with_yield(object)
- yield(object)
- end
- count = 10000000
- Benchmark.bm do |x|
- object = Hash.new { |h,k| h[k] = 0 }
- x.report("call") { count.times { with_call(object) { |o| o[:call] += 1 } } }
- x.report("proc") { count.times { with_proc(object) { |o| o[:proc] += 1 } } }
- x.report("yield") { count.times { with_yield(object) { |o| o[:yield] += 1 } } }
- end
- # user system total real
- # call 9.950000 0.180000 10.130000 ( 10.157157)
- # proc 10.740000 0.140000 10.880000 ( 10.899302)
- # yield 2.470000 0.000000 2.470000 ( 2.480088)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement