After reading this article, you’ll learn: –
- How to calculate the program’s execution time in Python
- Measure the total time elapsed to execute the code block in seconds, milliseconds, minutes, and hours
- Also, get the execution time of functions and loops.
In this article, We will use the following four ways to measure the execution time in Python: –
time.time()
function: measure the the total time elapsed to execute the script in seconds.time.process_time()
: measure the CPU execution time of a code- timeit module: measure the execution time of a small piece of a code including the single line of code as well as multiple lines of code
- DateTime module: measure the execution time in the hours-minutes-seconds format.
To measure the code performance, we need to calculate the time taken by the script/program to execute. Measuring the execution time of a program or parts of it will depend on your operating system, Python version, and what you mean by ‘time’.
Before proceeding further, first, understand what time is.
Table of contents
Wall time vs. CPU time
We often come across two terms to measure the execution time: Wall clock time and CPU time.
So it is essential to define and differentiate these two terms.
- Wall time (also known as clock time or wall-clock time) is simply the total time elapsed during the measurement. It’s the time you can measure with a stopwatch. It is the difference between the time at which a program finished its execution and the time at which the program started. It also includes waiting time for resources.
- CPU Time, on the other hand, refers to the time the CPU was busy processing the program’s instructions. The time spent waiting for other task to complete (like I/O operations) is not included in the CPU time. It does not include the waiting time for resources.
The difference between the Wall time and CPU time can occur from architecture and run-time dependency, e.g., programmed delays or waiting for system resources to become available.
For example, a program reports that it has used “CPU time 0m0.2s, Wall time 2m4s”. It means the program was active for 2 minutes and four seconds. Still, the computer’s processor spent only 0.2 seconds performing calculations for the program. May be program was waiting for some resources to become available.
At the beginning of each solution, I listed explicitly which kind of time each method measures.
So depending upon why you are measuring your program’s execution time, you can choose to calculate the Wall or CPU time.
How to Measure Execution Time in Python
The Python time module provides various time-related functions, such as getting the current time and suspending the calling thread’s execution for the given number of seconds. The below steps show how to use the time module to calculate the program’s execution time.
- Import time module
The time module comes with Python’s standard library. First, Import it using the import statement.
- Store the start time
Now, we need to get the start time before executing the first line of the program. To do this, we will use the
time()
function to get the current time and store it in a ‘start_time‘ variable before the first line of the program.
Thetime()
function of a time module is used to get the time in seconds since epoch. The handling of leap seconds is platform-dependent. - Store the end time
Next, we need to get the end time before executing the last line.
Again, we will use thetime()
function to get the current time and store it in the ‘end_time‘ variable before the last line of the program. - Calculate the execution time
The difference between the end time and start time is the execution time. Get the execution time by subtracting the start time from the end time.
Example: Get Program’s Execution Time in Seconds
Use this solution in the following cases: –
- Determine the execution time of a script
- Measure the time taken between lines of code.
Note: This solution measures the Wall time, i.e., total elapsed time, not a CPU time.
import time
# get the start time
st = time.time()
# main program
# find sum to first 1 million numbers
sum_x = 0
for i in range(1000000):
sum_x += i
# wait for 3 seconds
time.sleep(3)
print('Sum of first 1 million numbers is:', sum_x)
# get the end time
et = time.time()
# get the execution time
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')
Output:
Sum of first 1 million numbers is: 499999500000 Execution time: 3.125561475753784 seconds
Note: It will report more time if your computer is busy with other tasks. If your script was waiting for some resources, the execution time would increase because the waiting time will get added to the final result.
Get Execution Time in Milliseconds
Use the above example to get the execution time in seconds, then multiply it by 1000 to get the final result in milliseconds.
Example:
# get execution time in milliseconds
res = et - st
final_res = res * 1000
print('Execution time:', final_res, 'milliseconds')
Output:
Sum of first 1 million numbers is: 499999500000
Execution time: 3125.988006591797 milliseconds
Get Execution Time in Minutes
Use the above example to get the execution time in seconds, then divide it by 60 to get the final result in minutes.
Example:
# get execution time in minutes
res = et - st
final_res = res / 60
print('Execution time:', final_res, 'minutes')
Output:
Sum of first 1 million numbers is: 499999500000 Execution time: 0.05200800895690918 minutes
Do you want better formatting?
Use the strftime() to convert the time in a more readable format like (hh-mm-ss) hours-minutes-seconds.
import time
st = time.time()
# your code
sum_x = 0
for i in range(1000000):
sum_x += i
time.sleep(3)
print('Sum:', sum_x)
elapsed_time = time.time() - st
print('Execution time:', time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))
Output:
Sum: 499999500000
Execution time: 00:00:03
Get Program’s CPU Execution Time using process_time()
The time.time()
will measure the wall clock time. If you want to measure the CPU execution time of a program use the time.process_time()
instead of time.time()
.
Use this solution if you don’t want to include the waiting time for resources in the final result. Let’s see how to get the program’s CPU execution time.
import time
# get the start time
st = time.process_time()
# main program
# find sum to first 1 million numbers
sum_x = 0
for i in range(1000000):
sum_x += i
# wait for 3 seconds
time.sleep(3)
print('Sum of first 1 million numbers is:', sum_x)
# get the end time
et = time.process_time()
# get execution time
res = et - st
print('CPU Execution time:', res, 'seconds')
Output:
Sum of first 1 million numbers is: 499999500000 CPU Execution time: 0.234375 seconds
Note:
Because we are calculating the CPU execution time of a program, as you can see, the program was active for more than 3 seconds. Still, those 3 seconds were not added in CPU time because the CPU was ideal, and the computer’s processor spent only 0.23 seconds performing calculations for the program.
timeit module to measure the execution time of a code
Python timeit module provides a simple way to time small piece of Python code. It has both a Command-Line Interface as well as a callable one. It avoids many common traps for measuring execution times.
timeit module is useful in the following cases: –
- Determine the execution time of a small piece of code such as functions and loops
- Measure the time taken between lines of code.
The timeit()
function: –
The timeit.timeit()
returns the time (in seconds) it took to execute the code number times.
timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000, globals=None)
Note: This solution measures the Wall time, i.e., total elapsed time, not a CPU time.
The below steps show how to measure the execution time of a code using the timeit module.
- First, create a Timer instance using the
timeit()
function - Next, Pass a code at the place of the
stmt
argument. stmt is the code for which we want to measure the time - Next, If you wish to execute a few statements before your actual code, pass them to the setup argument like import statements.
- To set a timer value, we will use the default timer provided by Python.
- Next, decide how many times you want to execute the code and pass it to the number argument. The default value of number is 1,000,000.
- In the end, we will execute the
timeit()
function with the above values to measure the execution time of the code
Example: Measure the execution time of a function
Here we will calculate the execution time of an ‘addition()’ function. We will run the addition()
function five-time to get the average execution time.
import timeit
# print addition of first 1 million numbers
def addition():
print('Addition:', sum(range(1000000)))
# run same code 5 times to get measurable data
n = 5
# calculate total execution time
result = timeit.timeit(stmt='addition()', globals=globals(), number=n)
# calculate the execution time
# get the average execution time
print(f"Execution time is {result / n} seconds")
Output:
Addition: 499999500000 Addition: 499999500000 Addition: 499999500000 Addition: 499999500000 Addition: 499999500000 Execution time is 0.03770382 seconds
Note:
If you run time-consuming code with the default number
value, it will take a lot of time. So assign less value to the number
argument Or decide how many samples do you want to measure to get the accurate execution time of a code.
- The
timeit()
functions disable the garbage collector, which results in accurate time capture. - Also, using the
timeit()
function, we can repeat the execution of the same code as many times as we want, which minimizes the influence of other tasks running on your operating system. Due to this, we can get the more accurate average execution time.
Measure the execution time of a single line of code
Run the %timeit
command on a command-line or jupyter notebook to get the execution time of a single line of code.
Example: Use %timeit
just before the line of code
%timeit [x for x in range(1000)]
# Output
2.08 µs ± 223 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Also, we can customize the command using the various options to enhance the profiling and capture a more accurate execution time.
- Define the number of runs using the
-r
option. For example,%timeit -r10 your_code
means run the code line 10 times. - Define the loops within each run using the
-r
and-n
option. - If you ommit the options be default it is 7 runs with each run having 1 million loops
Example: Customize the time profile operation to 10 runs and 20 loops within each run.
# Customizing number of runs and loops in %timeit
%timeit -r10 -n20 [x for x in range(1000)]
# output
1.4 µs ± 12.34 ns per loop (mean ± std. dev. of 10 runs, 20 loops each)
Measure the execution time of a multiple lines of code
Using the %%timeit
command, we can measure the execution time of multiple lines of code. The command options will remain the same.
Note: you need to replace the single percentage (%
) with double percentage (%%
) in the timeit command to get the execution time of multiple lines of a code
Example:
# Time profiling using %%timeit
%%timeit -r5 -n10
# find sum to first 1 million numbers
sum_x = 0
for i in range(1000000):
sum_x += i
# Output
10.5 µs ± 226 ns per loop (mean ± std. dev. of 5 runs, 10 loops each)
DateTime Module to determine the script’s execution time
Also, you can use the Datetime module to measure the program’s running time. Use the below steps.
Import DateTime module
- Next, store the start time using the
datetime.now()
function before the first line of a script - Next, save the end time before using the same function before the last line of a script
- In the end, calculate the execution time by subtracting the start time from an end time
Note: This solution measures the Wall time, i.e., total elapsed time, not a CPU time.
Example:
import datetime
import time
# get the start datetime
st = datetime.datetime.now()
# main program
# find sum to first 1 million numbers
sum_x = 0
for i in range(1000000):
sum_x += i
# wait for 3 seconds
time.sleep(3)
print('Sum of first 1 million numbers is:', sum_x)
# get the end datetime
et = datetime.datetime.now()
# get execution time
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')
Output:
Sum of first 1 million numbers is: 499999500000 Execution time: 0:00:03.115498 seconds
Conclusion
Python provides several functions to get the execution time of a code. Also, we learned the difference between Wall-clock time and CPU time to understand which execution time we need to measure.
Use the below functions to measure the program’s execution time in Python:
time.time()
: Measure the the total time elapsed to execute the code in seconds.timeit.timeit()
: Simple way to time a small piece of Python code%timeit
and%%timeit
: command to get the execution time of a single line of code and multiple lines of code.datetime.datetime.now()
: Get execution time in hours-minutes-seconds format
Also, use the time.process_time()
function to get the program’s CPU execution time.
Can we estimate time to run a code before actually runnung it?
Running a code depends on various factors such as the availability of resources (CPU, memory), so I don’t think it’s possible.