Skip to content

Instantly share code, notes, and snippets.

@talonsensei
Created September 16, 2015 20:00
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 talonsensei/4675aee6841a90f2b77f to your computer and use it in GitHub Desktop.
Save talonsensei/4675aee6841a90f2b77f to your computer and use it in GitHub Desktop.
Display elapsed time in human readable format
import time
###############################################
# Returns elapsed time dynamically formatted #
# to accomodate days, hours, minutes, seconds #
###############################################
def elapsed_time(start,end):
# For use with time.time() values as start and end
units = [" days "," hours "," minutes ", " seconds"]
delta = end - start
sec = "{:.3f}".format(delta % 60) # Format seconds to 3 decimals
minutes = str(int(delta // 60 % 60))
hours = str(int(delta // 3600 % 24))
days = str(int(delta // 86400))
time_val = [days,hours,minutes,sec]
time_passed = ""
for i in range(0,4):
if time_val[i] != '0': time_passed = time_passed + time_val[i]+units[i]
return time_passed
start_time = time.time()
# Do something long here...
end_time = time.time()
print "Elapsed time: ", elapsed_time(start_time,finish_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment