Home >Backend Development >Python Tutorial >How Can I Measure Function Execution Time in Python?

How Can I Measure Function Execution Time in Python?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-18 17:09:11959browse

How Can I Measure Function Execution Time in Python?

Measuring Elapsed Time in Python

To gauge the execution time of a function, consider utilizing time.time() to determine the elapsed wall-clock time between two points.

import time

start = time.time()
print("hello")
end = time.time()
print(end - start)

This approach yields the execution time in seconds.

Alternately, you may consider employing perf_counter or process_time from Python 3.3 onward, contingent on your specific requirements. Prior to this version, time.clock was recommended for this purpose, though it has since been deprecated in favor of the aforementioned options.

The above is the detailed content of How Can I Measure Function Execution Time in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn