Home >Backend Development >Python Tutorial >How Can I Efficiently Measure the Total Execution Time of My Python Program?
In Python, quantifying the execution time of a program is a common requirement, particularly for performance optimization and debugging purposes. This article addresses the specific challenge of measuring the total execution time of a Python script.
While the timeit module is efficient for timing small code snippets, it's not suitable for measuring the runtime of an entire program. Hence, we present a straightforward approach using the time module:
import time # Record the start time start_time = time.time() # Execute the main program logic main() # Get the elapsed time elapsed_time = time.time() - start_time # Print the result print("--- %s seconds ---" % elapsed_time)
This approach assumes that your program takes at least a tenth of a second to run. The elapsed time is then printed in seconds, accounting for the time it takes to execute the entire script. This method provides a clear and concise way to determine the total execution time of your Python programs.
The above is the detailed content of How Can I Efficiently Measure the Total Execution Time of My Python Program?. For more information, please follow other related articles on the PHP Chinese website!