Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.