Use time.time() to measure the elapsed wall-clock time between two points:
import time
start = time.time()
print("hello")
end = time.time()
print(end - start)
This gives the execution time in seconds.
Another option since Python 3.3 might be to use perf_counter or process_time, depending on your requirements. Before 3.3 it was recommended to use time.clock (thanks Amber). However, it is currently deprecated:
Answer from NPE on Stack OverflowOn Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.
On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function
QueryPerformanceCounter(). The resolution is typically better than one microsecond.Deprecated since version 3.3: The behaviour of this function depends on the platform: use
perf_counter()orprocess_time()instead, depending on your requirements, to have a well defined behaviour.
Use time.time() to measure the elapsed wall-clock time between two points:
import time
start = time.time()
print("hello")
end = time.time()
print(end - start)
This gives the execution time in seconds.
Another option since Python 3.3 might be to use perf_counter or process_time, depending on your requirements. Before 3.3 it was recommended to use time.clock (thanks Amber). However, it is currently deprecated:
On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.
On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function
QueryPerformanceCounter(). The resolution is typically better than one microsecond.Deprecated since version 3.3: The behaviour of this function depends on the platform: use
perf_counter()orprocess_time()instead, depending on your requirements, to have a well defined behaviour.
Use timeit.default_timer instead of timeit.timeit. The former provides the best clock available on your platform and version of Python automatically:
from timeit import default_timer as timer
start = timer()
# ...
end = timer()
print(end - start) # Time in seconds, e.g. 5.38091952400282
timeit.default_timer is assigned to time.time() or time.clock() depending on OS. On Python 3.3+ default_timer is time.perf_counter() on all platforms. See Python - time.clock() vs. time.time() - accuracy?
See also:
- Optimizing code
- How to optimize for speed
Videos
Hello,
I've written a few different implementations of a program that I would like to do some benchmark comparisons. I have a list of growing inputs and a set of configurations to run. I am iterating over the growing inputs and then iterating over the configurations internally.
I used cProfile to get general performance comparisons, but I'd like to generate some performance curves for each program.
I've written a simple decorator that modifies the output to return the return value and runtime.
However, I'd like the program to raise an Exception if the program takes too long. That way I can catch the exception, complete the current iteration of configurations and then kill the loop.
I haven't found anything like this online, but is there anything like this? It seems like it would be a pretty generic utility function. How could/should I implement a timeout decorator?
Here is a snippet of my benchmarking iteration:
# if the function ever times out, kill the loop after completing for all starting positions
timeoutFailure = False
for turn in turns:
results = [turn]
for case in cases:
try:
return value, runtime = testFunction(case, turn)
except Exception:
runtime = -1
timeoutFailure = True
results = results + [runtime]
BenchmarkWriter.writerow(results)
if timeoutFailure:
breakAnd here is my benchmarking decorator:
import random
import time
def timerfunc(func):
"""
A timer decorator
"""
def function_timer(*args, **kwargs):
"""
A nested function for timing other functions
"""
start = time.time()
value = func(*args, **kwargs)
end = time.time()
runtime = end - start
return value, runtime
return function_timer