Last active
January 4, 2016 02:19
-
-
Save 404pnf/8554060 to your computer and use it in GitHub Desktop.
ruby function to time a function
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
# 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