The timedelta object in Python is used to represent a duration or difference between two
dates or times. We can specify the duration in terms of days, seconds, microseconds, milliseconds, minutes, hours, and weeks.
It is commonly used for date and time arithmetic.
In the example below, you can see how long an operation takes to run by using the timedelta
object to calculate the difference between the start and end times.
import datetime
timestamp1 = datetime.datetime.now() # start time
# some code to run
for i in range(1000000):
pass
timestamp2 = datetime.datetime.now() # end time
duration = timestamp2 - timestamp1
print(f"Duration: {duration}")
This task took approximately 0.063 seconds to complete.