Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I get time of a Python program\'s execution?
Python provides different ways to measure the execution time of a script or specific code segments. The most common approaches use functions from the time module like time.time() and time.process_time(), or the dedicated timeit module for precise measurements.
Here are the main methods to measure execution time in Python:
Using time.time() Function
The time.time() function returns the current time as a floating-point number representing seconds since the Unix epoch (January 1, 1970). This method measures wall-clock time ? the actual time that passes in the real world.
To calculate execution time, call time.time() before and after the code you want to measure, then subtract the start time from the end time.
Example
Here's how to measure the execution time of a simple operation ?
import time
# Record start time
start_time = time.time()
# Code to measure
print("Hello, World!")
result = sum(range(1000))
# Record end time
end_time = time.time()
print("Execution time:", end_time - start_time, "seconds")
The output shows the wall-clock time taken ?
Hello, World! Execution time: 0.0001220703125 seconds
Using time.process_time() Function
The time.process_time() function measures only the CPU time used by the current process. Unlike time.time(), it excludes time spent waiting for I/O operations, sleeping, or other system delays.
This method is ideal for measuring computational performance since it focuses purely on processing time.
Example
Let's measure CPU time for a computation-heavy task ?
import time
# Record CPU start time
start_cpu = time.process_time()
# Computational task
squares = [x*x for x in range(100000)]
total = sum(squares)
# Record CPU end time
end_cpu = time.process_time()
print("Sum of squares:", total)
print("CPU Time:", end_cpu - start_cpu, "seconds")
The output shows only the CPU processing time ?
Sum of squares: 333328333350000 CPU Time: 0.046875 seconds
Using timeit Module
The timeit module provides the most accurate way to measure execution time for small code snippets. It runs the code multiple times and returns precise timing measurements, automatically handling timing overhead.
Example
Here's how to use timeit.timeit() to measure a function's execution time ?
import timeit
def calculate_factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
# Measure execution time (runs 1000 times by default)
execution_time = timeit.timeit(lambda: calculate_factorial(10), number=10000)
print("Average execution time:", execution_time / 10000, "seconds")
print("Total time for 10000 runs:", execution_time, "seconds")
The output shows precise timing measurements ?
Average execution time: 1.2345e-06 seconds Total time for 10000 runs: 0.012345 seconds
Comparison of Methods
| Method | Measures | Best For | Accuracy |
|---|---|---|---|
time.time() |
Wall-clock time | Overall program duration | Good |
time.process_time() |
CPU time only | Computational performance | Better |
timeit |
Precise timing | Small code snippets | Highest |
Conclusion
Use time.time() for measuring overall program duration, time.process_time() for pure computational time, and timeit for accurate measurements of small code snippets. Choose the method based on whether you need wall-clock time, CPU time, or high-precision measurements.
