How do I get time of a Python program's execution?

To get the time it takes for a Python program to execute, you can use the time module. Here is an example of how you can use it:

import time

start_time = time.time()

i = 1
for i in range(1000000):
    i += 1

end_time = time.time()
time_elapsed = end_time - start_time
print("Time taken: ", time_elapsed)

Watch a course Python - The Practical Guide

The time_elapsed variable will contain the time in seconds it took for the code to execute. If you want the time in a more readable format, you can use the datetime module to convert the elapsed time to hours, minutes, and seconds.

import time
import datetime

def convert_seconds(seconds):
    hours, seconds = divmod(seconds, 3600)
    minutes, seconds = divmod(seconds, 60)
    return f"{int(hours)} hours {int(minutes)} minutes {int(seconds)} seconds"

start_time = time.time()

i = 1
for i in range(1000000):
    i += 1

end_time = time.time()
time_elapsed = end_time - start_time
print("Time taken: ", time_elapsed)

print(convert_seconds(time_elapsed))

This will print the time taken in a more readable format, like "2 hours 30 minutes 15 seconds".