Measuring Elapsed Execution Time in Python
To determine the execution duration of a function, timeit may not always be the ideal choice. Instead, for precise timekeeping, leverage the time.time() method. It measures the elapsed wall-clock time between two timestamps effectively.
Consider the following example:
import time start = time.time() print("hello") end = time.time() print(end - start)
This snippet calculates the time taken to execute the print() statement and displays the result in seconds.
Additionally, Python 3.3 introduced perf_counter and process_time, tailored to specific time measurement needs. Prior to Python 3.3, time.clock was recommended, but it has since been deprecated.
The above is the detailed content of How Can I Accurately Measure the Execution Time of a Python Function?. For more information, please follow other related articles on the PHP Chinese website!