Reputation: 59345
I'm new to Python, and confused by the date/time documentation. I want to compute the time that it takes to perform a computation.
In java, I would write:
long timeBefore = System.currentTimeMillis();
doStuff();
long timeAfter = System.currentTimeMillis();
elapsed time = timeAfter - timeBefore;
I'm sure it's even easier in Python. Can anyone help?
Upvotes: 63
Views: 147789
Reputation: 1286
I imagine two solutions with Timer for Python:
Timer for Python is based on time.perf_counter_ns()
, so it's precise down to the nanosecond (though you rarely find a Python function that fast).
@function_timer()
Simply use @function_timer()
as decoration on top of your function:
import time
from timer import function_timer
@function_timer()
def do_stuff():
time.sleep(1)
print("Done")
do_stuff()
The terminal output would be something like this:
Done
Elapsed time: 1.01 seconds for thread DO_STUFF
with
StatementSimply add with Timer():
before you run your main function:
import time
from timer import Timer
def do_stuff():
time.sleep(1)
print("Done")
with Timer():
do_stuff()
The terminal output would be something like this:
Done
Elapsed time: 1.00 seconds
PS: In full disclosure, I'm the author of Timer for Python, a lightweight package that makes it easy to measure time and performance of code.
Upvotes: -1
Reputation: 20125
For Python 3.3 and later time.process_time()
is very nice:
import time
t = time.process_time()
#do some stuff
elapsed_time = time.process_time() - t
When it comes to performance measurement, you likely need this because
[it] return[s] the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.
Upvotes: 16
Reputation: 8546
I also got a requirement to calculate the process time of some code lines. So I tried the approved answer and I got this warning.
DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
So python will remove time.clock() from Python 3.8. You can see more about it from issue #13270. This warning suggest two function instead of time.clock(). In the documentation also mention about this warning in-detail in time.clock() section.
Deprecated since version 3.3, will be removed in version 3.8: The behaviour of this function depends on the platform: use perf_counter() or process_time() instead, depending on your requirements, to have a well defined behaviour.
Let's look at in-detail both functions.
Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.
New in version 3.3.
Changed in version 3.10: On Windows, the function is now system-wide.
So if you want it as nanoseconds
, you can use time.perf_counter_ns() and if your code consist with time.sleep(secs), it will also count. Ex:-
import time
def func(x):
time.sleep(5)
return x * x
lst = [1, 2, 3]
tic = time.perf_counter()
print([func(x) for x in lst])
toc = time.perf_counter()
print(toc - tic)
# [1, 4, 9]
# 15.0041916 --> output including 5 seconds sleep time
Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.
Use process_time_ns() to avoid the precision loss caused by the float type.
New in version 3.3.
So if you want it as nanoseconds
, you can use time.process_time_ns() and if your code consist with time.sleep(secs), it won't count. Ex:-
import time
def func(x):
time.sleep(5)
return x * x
lst = [1, 2, 3]
tic = time.process_time()
print([func(x) for x in lst])
toc = time.process_time()
print(toc - tic)
# [1, 4, 9]
# 0.0 --> output excluding 5 seconds sleep time
Please note both time.perf_counter_ns() and time.process_time_ns() come up with Python 3.7 onward.
Upvotes: 2
Reputation: 319531
Equivalent in python would be:
>>> import time
>>> tic = time.clock()
>>> toc = time.clock()
>>> toc - tic
If you are trying to find the best performing method then you should probably have a look at timeit
.
Upvotes: 97
Reputation: 3419
If all you want is the time between two points in code (and it seems that's what you want) I have written tic()
toc()
functions ala Matlab's implementation. The basic use case is:
tic()
''' some code that runs for an interesting amount of time '''
toc()
# OUTPUT:
# Elapsed time is: 32.42123 seconds
Super, incredibly easy to use, a sort of fire-and-forget kind of code. It's available on Github's Gist https://gist.github.com/tyleha/5174230
Upvotes: 1
Reputation: 467
You can implement two tic()
and tac()
functions, where tic()
captures the time which it is called, and tac()
prints the time difference since tic()
was called. Here is a short implementation:
import time
_start_time = time.time()
def tic():
global _start_time
_start_time = time.time()
def tac():
t_sec = round(time.time() - _start_time)
(t_min, t_sec) = divmod(t_sec,60)
(t_hour,t_min) = divmod(t_min,60)
print('Time passed: {}hour:{}min:{}sec'.format(t_hour,t_min,t_sec))
Now in your code you can use it as:
tic()
do_some_stuff()
tac()
and it will, for example, output:
Time passed: 0hour:7min:26sec
Upvotes: 16
Reputation: 1307
For some further information on how to determine the processing time, and a comparison of a few methods (some mentioned already in the answers of this post) - specifically, the difference between:
start = time.time()
versus the now obsolete (as of 3.3, time.clock() is deprecated)
start = time.clock()
see this other article on Stackoverflow here:
Python - time.clock() vs. time.time() - accuracy?
If nothing else, this will work good:
start = time.time()
... do something
elapsed = (time.time() - start)
Upvotes: 1
Reputation: 6220
Building on and updating a number of earlier responses (thanks: SilentGhost, nosklo, Ramkumar) a simple portable timer would use timeit
's default_timer()
:
>>> import timeit
>>> tic=timeit.default_timer()
>>> # Do Stuff
>>> toc=timeit.default_timer()
>>> toc - tic #elapsed time in seconds
This will return the elapsed wall clock (real) time, not CPU time. And as described in the timeit
documentation chooses the most precise available real-world timer depending on the platform.
ALso, beginning with Python 3.3 this same functionality is available with the time.perf_counter
performance counter. Under 3.3+ timeit.default_timer() refers to this new counter.
For more precise/complex performance calculations, timeit
includes more sophisticated calls for automatically timing small code snippets including averaging run time over a defined set of repetitions.
Upvotes: 41