How to check the Execution Time of Python script ?
Last Updated :
27 Jun, 2025
Checking the execution time of a python script helps you measure how long a piece of code takes to run. This is useful for analyzing performance, especially when working with large data or complex algorithms.
Let's explore different ways to check execution time of python.
Using timeit module
timeit module is used to measure how fast a small piece of Python code runs. It runs the code multiple times and gives the average time, which makes the result more accurate and reliable than using just start and end time.
To use timeit module we have to import it:
import timeit
Example 1: In code
Python
import timeit
a = '''
from math import sqrt
def example():
mylist = [sqrt(x) for x in range(100)]
'''
t = timeit.timeit(a, number=1000000) * 1e3
print(round(t, 3), "ms")
Explanation:
- timeit.timeit(code, number=1000000) runs the code 1 million times and measures the total time taken.
- * 1e3 converts the time from seconds to milliseconds and round(..., 3) prints it with 3 decimal places.
Example 2: In command line
timeit command can be used in the terminal to measure how fast a small piece of Python code runs. It allows testing code performance without creating a full Python script.
Common options include:
- -s for setup code (e.g., imports),
- -n to set the number of executions,
- -p to measure process time instead of wall-clock time.
timeit CLI statement:
python -m timeit "sum(range(100))"
Explanation: sum(range(100)) calculates the sum of numbers, -m timeit runs timeit module from the command line.
Output:
1000000 loops, best of 5: 0.261 usec per loop
Explanation: It shows how many times the code ran and the best average time per run in microseconds.
Using time module
time module can be used to measure how long a script takes to run. Record the start time before the code and the end time after it, then subtract them to get the execution time.
To use time module we have to import it:
import time
Example 1: Measuring Start-End time.
Python
import time
s = time.time()
a = sum(i**100 for i in range(1000))
print((time.time() - s) * 1e3, "ms")
Output1.5659332275390625 ms
Explanation:
- time.time() records the start time.
- sum(i**100 for i in range(1000)) calculates the sum of each number from 0 to 999 raised to the power 100.
- (time.time()- s) * 1e3 measures the time difference and converts time to milliseconds (1e3 means 1000).
Example 2: Time per Iteration.
Python
import time
for j in range(100, 5501, 100):
s = time.time()
t = sum(i**100 for i in range(j))
e = time.time()
print("Iteration:", j, "Time taken:", round((e - s) * 1000, 3), "ms")
OutputIteration: 100 Time taken: 0.131ms
Iteration: 200 Time taken: 0.271ms
Iteration: 300 Time taken: 0.542ms
Iteration: 400 Time taken: 0.558ms
Iteration: 500 Time taken: 0.741ms
Iteration: 600 Time taken...
Explanation:
- total = sum(i**100 for i in range(j)) calculates the power sum for each iteration.
- round((end - start) * 1000, 3) computes and prints execution time in milliseconds.
Using datetime module
datetime module measure execution time by taking the current time before and after code runs using datetime.now(), then finding the difference.
To use datetime module we have to import it:
from datetime import datetime
Python
from datetime import datetime
s = datetime.now()
a = sum(i**100 for i in range(1000))
print((datetime.now() - s).total_seconds() * 1e3)
Explanation:
- datetime.now() records the start time using the datetime module.
- sum(i**100 for i in range(1000)) calculates sum of each number from 0 to 999 raised to the power 100.
- (datetime.now() - s).total_seconds() * 1e3 finds the time difference, converts it to milliseconds.
Related Articles: