Guest User

Untitled

a guest
Jun 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. class Multibench
  2. def initialize
  3. @benches = Hash.new { 0 }
  4. end
  5.  
  6. def bench(title)
  7. start = Time.now
  8. yield.tap do
  9. @benches[title] += Time.now - start
  10. end
  11. end
  12.  
  13. def benches
  14. @benches.sort_by { |title, time| -time }
  15. end
  16. end
  17.  
  18. if __FILE__ == $0
  19. multibench = Multibench.new
  20.  
  21. multibench.bench("foo") { sleep(0.1) }
  22. multibench.bench("bar") { sleep(0.3) }
  23. multibench.bench("foo") { sleep(0.1) }
  24.  
  25. multibench.benches.each do |title_and_time|
  26. puts "%-20s %.2fs" % title_and_time
  27. end
  28. end
Add Comment
Please, Sign In to add comment