Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Multibench
- def initialize
- @benches = Hash.new { 0 }
- end
- def bench(title)
- start = Time.now
- yield.tap do
- @benches[title] += Time.now - start
- end
- end
- def benches
- @benches.sort_by { |title, time| -time }
- end
- end
- if __FILE__ == $0
- multibench = Multibench.new
- multibench.bench("foo") { sleep(0.1) }
- multibench.bench("bar") { sleep(0.3) }
- multibench.bench("foo") { sleep(0.1) }
- multibench.benches.each do |title_and_time|
- puts "%-20s %.2fs" % title_and_time
- end
- end
Add Comment
Please, Sign In to add comment