How to Measure Elapsed Time in Python

One of the most common tasks in programming is measuring the time it takes for a program to execute. This is particularly useful when you want to optimize your code or compare the performance of different algorithms. In this tutorial, you will learn how to measure elapsed time in Python using various methods.

Method 1: Using the time module

The simplest way to measure elapsed time in Python is to use the time module. This module provides various functions for working with time, including getting the current time and measuring the time it takes for a piece of code to execute.

Step 1: Import the time module

To use the time module, you first need to import it. You can do this by adding the following line at the beginning of your code:

``import time``

Step 2: Get the start time

To measure the elapsed time, you need to get the start time before the code you want to measure. You can do this by calling the time() function, which returns the current time in seconds since the epoch (January 1, 1970, 00:00:00 UTC).

``start_time = time.time()``

Step 3: Run the code you want to measure

Next, you need to run the code you want to measure. This can be any code, such as a loop or a function.

``````for i in range(1000000):
pass``````

Step 4: Get the end time

After the code has finished running, you need to get the end time. You can do this by calling the time() function again.

``end_time = time.time()``

Step 5: Calculate the elapsed time

Finally, you can calculate the elapsed time by subtracting the start time from the end time.

``````elapsed_time = end_time - start_time
print("Elapsed time:", elapsed_time)``````

This will output the elapsed time in seconds.

Method 2: Using the datetime module

Another way to measure elapsed time in Python is to use the datetime module. This module provides various functions for working with dates and times, including measuring the time it takes for a piece of code to execute.

Step 1: Import the datetime module

To use the datetime module, you need to import it. You can do this by adding the following line at the beginning of your code:

``import datetime``

Step 2: Get the start time

To measure the elapsed time, you need to get the start time before the code you want to measure. You can do this by creating a datetime object using the now() function.

``start_time = datetime.datetime.now()``

Step 3: Run the code you want to measure

Next, you need to run the code you want to measure. This can be any code, such as a loop or a function.

``````for i in range(1000000):
pass``````

Step 4: Get the end time

After the code has finished running, you need to get the end time. You can do this by creating another datetime object using the now() function.

``end_time = datetime.datetime.now()``

Step 5: Calculate the elapsed time

Finally, you can calculate the elapsed time by subtracting the start time from the end time.

``````elapsed_time = end_time - start_time
print("Elapsed time:", elapsed_time)``````

This will output the elapsed time in days, seconds, and microseconds.

In this tutorial, you learned two methods for measuring elapsed time using the time and datetime modules. You can choose the method that best suits your needs depending on the precision and readability you require.