Skip to content

Instantly share code, notes, and snippets.

@aalin
Created October 26, 2010 09:52
Show Gist options
  • Select an option

  • Save aalin/646619 to your computer and use it in GitHub Desktop.

Select an option

Save aalin/646619 to your computer and use it in GitHub Desktop.
multibench: useful for benchmarking columns in html tables for example.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment