-
-
Save eladmeidar/6525567 to your computer and use it in GitHub Desktop.
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
# timeit { sleep 20 } | |
# => {:weeks=>0, :days=>0, :hours=>0, :minutes=>0, :seconds=>20} | |
def timeit(&block) | |
mul = [604800, 86400, 3600, 60, 1] | |
values = [:weeks, :days, :hours, :minutes, :seconds] | |
units = [] | |
started = Time.now.to_i | |
yield block | |
ended = Time.now.to_i | |
total = (ended - started).to_f.round | |
mul.inject(total) do |t, m| | |
units << t / m | |
t % m | |
end | |
Hash[values.zip(units)] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment