Last active
December 23, 2015 11:59
-
-
Save havenwood/6632658 to your computer and use it in GitHub Desktop.
Clojure's `time` in Ruby for a bit of benchmarking sugar.
This file contains 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
require 'benchmark' | |
module Kernel | |
## | |
# Evaluates expr and prints the time it took. Returns the value of expr. | |
def time | |
value = nil | |
msecs = Benchmark.realtime do | |
value = yield | |
end | |
puts "Elapsed time: #{msecs} msecs" | |
value | |
end | |
end | |
## | |
# Example usage: | |
time do | |
sleep 5 | |
1 + 1 | |
end | |
#>> Elapsed time: 5.00057 msecs | |
#=> 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment