Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'benchmark'
  4.  
  5. def with_call(object, &block)
  6. block.call(object)
  7. end
  8.  
  9. def with_proc(object)
  10. Proc.new.call(object)
  11. end
  12.  
  13. def with_yield(object)
  14. yield(object)
  15. end
  16.  
  17. count = 10000000
  18.  
  19. Benchmark.bm do |x|
  20. object = Hash.new { |h,k| h[k] = 0 }
  21.  
  22. x.report("call") { count.times { with_call(object) { |o| o[:call] += 1 } } }
  23. x.report("proc") { count.times { with_proc(object) { |o| o[:proc] += 1 } } }
  24. x.report("yield") { count.times { with_yield(object) { |o| o[:yield] += 1 } } }
  25. end
  26.  
  27. # user system total real
  28. # call 9.950000 0.180000 10.130000 ( 10.157157)
  29. # proc 10.740000 0.140000 10.880000 ( 10.899302)
  30. # yield 2.470000 0.000000 2.470000 ( 2.480088)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement