Created
October 26, 2010 09:52
-
-
Save aalin/646619 to your computer and use it in GitHub Desktop.
multibench: useful for benchmarking columns in html tables for example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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