[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)

There are already many articles on how to measure elapsed time using the time module in a Python program, and I learned a lot. I haven't come up with a way to display hours: minutes: seconds like "03:14:15", so I'll write it down.

That said, it just uses the time module to calculate the elapsed time in seconds and convert it to muddy.

import time

# get time when the program stated
start_time = time.time()
    
"""
Your program here
""" 
    
# calculate elapsed time
elapsed_time = int(time.time() - start_time)
    
# convert second to hour, minute and seconds
elapsed_hour = elapsed_time // 3600
elapsed_minute = (elapsed_time % 3600) // 60
elapsed_second = (elapsed_time % 3600 % 60)
    
# print as 00:00:00
print(str(elapsed_hour).zfill(2) + ":" + str(elapsed_minute).zfill(2) + ":" + str(elapsed_second).zfill(2))

■ Measurement of elapsed time

Get the current time with time.time (). Do this before and after the program starts and calculate the difference. Since small numbers such as milliseconds are unnecessary, it is converted to int type.

■ Conversion from seconds to hours, minutes, and seconds

When the quotient of seconds divided by 3600, Divide the quotient by dividing the remainder by 60, The remainder is seconds, It is said.

■ Display at 00:00:00

I use .zfill () to prefix each number with a single digit if it is a single digit. It seems that .zfill () can be used for str type. In this case, we want to use 2 digits, so we use .zfill (2).

* Reference article

Python Tips: I want to do zero padding with Python

Recommended Posts

[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Just print Python elapsed time in seconds
To represent date, time, time, and seconds in Python
How to get the date and time difference in seconds with python
Get the current date and time in Python, considering the time difference
Determine the date and time format in Python and convert to Unixtime
How to display bytes in the same way in Java and Python
Display Python 3 in the browser with MAMP
MongoDB for the first time in Python
Get and convert the current time in the system local timezone with python
Receive and display HTML form data in Python
About the difference between "==" and "is" in python
[Python] Display the Altair legend in the plot area
I want to display the progress in Python!
Convert timezoned date and time to Unixtime in Python2.7
Display n digits after the decimal point in python
The simplest Python memo in Japan (classes and objects)
Receive the form in Python and do various things
[Python] Measures and displays the time required for processing
Output the time from the time the program was started in python
Carefully understand the exponential distribution and draw in Python
Read the csv file and display it in the browser
Plot and understand the multivariate normal distribution in Python
Carefully understand the Poisson distribution and draw in Python
Find the Hermitian matrix and its eigenvalues in Python
Python OpenCV tried to display the image in text.
[Python] Convert time display (str type) using "" "and"'" to seconds (float type) with datetime and timedelta
Check the processing time and the number of calls for each process in python (cProfile)
Script to count and stop up to 5 seconds in Python in Blender
Download the file in Python
Find the difference in Python
Application to display and search local memos (diary) in Python
Display UTM-30LX data in Python
[Python] Read the csv file and display the figure with matplotlib
Get the MIME type in Python and determine the file format
Make a given number of seconds into hours, minutes and seconds
Sort and output the elements in the list as elements and multiples in Python.
Display numbers and letters assigned to variables in python print
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
[Understand in the shortest time] Python basics for data analysis
Manipulate the clipboard in Python and paste the table into Excel
I tried to illustrate the time and time in C language
I tried programming the chi-square test in Python and Java.
I tried to display the time and today's weather w
Display the time in Django according to the user's region (UTC)
Stack and Queue in Python
[Python] Strengths and weaknesses of DataFrame in terms of time required
Graph time series data in Python using pandas and matplotlib
[Tips] Problems and solutions in the development of python + kivy
Unittest and CI in Python
An easy way to view the time taken in Python and a smarter way to improve it
Graph the Poisson distribution and the Poisson cumulative distribution in Python and Java, respectively.
Put the process to sleep for a certain period of time (seconds) or more in Python
The story of returning to the front line for the first time in 5 years and refactoring Python Django
Count the number of Thai and Arabic characters well in Python
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Python beginners tried Hello World in 30 seconds using the micro-framework Flask
New Python grammar and features not mentioned in the introductory book
python> Display 3 decimals in ".3f, .3f, .3f" format / Get 3 coordinate values in the range [-1: 1]
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
DeepL translate YouTube Live chat and display it in real time