Skip to content

Instantly share code, notes, and snippets.

@404pnf
Last active January 4, 2016 02:19
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 404pnf/8554060 to your computer and use it in GitHub Desktop.
Save 404pnf/8554060 to your computer and use it in GitHub Desktop.
ruby function to time a function
# found at <https://github.com/jdantonio/functional-ruby/blob/master/lib/functional/utilities.rb>
# Run the given block and time how long it takes in seconds. All arguments
# will be passed to the block. The function will return two values. The
# first value will be the duration of the timer in seconds. The second
# return value will be the result of the block.
#
# @param args [Array] zero or more arguments to pass to the block
#
# @return [Integer, Object] the duration of the operation in seconds and
# the result of the block operation
def timer(*args)
return 0,nil unless block_given?
t1 = Time.now
result = yield(*args)
t2 = Time.now
return (t2 - t1), result
end
module_function :timer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment