Home > Backend Development > Python Tutorial > How Can I Accurately Measure the Execution Time of a Python Function?

How Can I Accurately Measure the Execution Time of a Python Function?

Patricia Arquette
Release: 2024-12-12 19:32:15
Original
384 people have browsed it

How Can I Accurately Measure the Execution Time of a Python Function?

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)
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template