Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active December 23, 2015 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/6632658 to your computer and use it in GitHub Desktop.
Save havenwood/6632658 to your computer and use it in GitHub Desktop.
Clojure's `time` in Ruby for a bit of benchmarking sugar.
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