If you want to measure CPU time, can use time.process_time() for Python 3.3 and above:
import time
start = time.process_time()
# your code here
print(time.process_time() - start)
First call turns the timer on, and second call tells you how many seconds have elapsed.
There is also a function time.clock(), but it is deprecated since Python 3.3 and will be removed in Python 3.8.
There are better profiling tools like timeit and profile, however time.process_time() will measure the CPU time and this is what you’re are asking about.
If you want to measure wall clock time instead, use time.time().
回答 1
您还可以使用time库:
import time
start = time.time()
# your code# end
print(f'Time: {time.time() - start}')
with CodeTimer('loop 1'):
for i inrange(100000):
passwith CodeTimer('loop 2'):
for i inrange(100000):
pass
Code block 'loop 1' took: 4.991 ms
Code block 'loop 2' took: 3.666 ms
并嵌套它们:
with CodeTimer('Outer'):
for i inrange(100000):
passwith CodeTimer('Inner'):
for i inrange(100000):
passfor i inrange(100000):
pass
Code block 'Inner' took: 2.382 ms
Code block 'Outer' took: 10.466 ms
You could then name the code blocks you want to measure:
with CodeTimer('loop 1'):
for i in range(100000):
pass
with CodeTimer('loop 2'):
for i in range(100000):
pass
Code block 'loop 1' took: 4.991 ms
Code block 'loop 2' took: 3.666 ms
And nest them:
with CodeTimer('Outer'):
for i in range(100000):
pass
with CodeTimer('Inner'):
for i in range(100000):
pass
for i in range(100000):
pass
Code block 'Inner' took: 2.382 ms
Code block 'Outer' took: 10.466 ms
Regarding timeit.default_timer(), it uses the best timer based on OS and Python version, see this answer.
回答 3
我总是喜欢以小时,分钟和秒(%H:%M:%S)格式检查时间:
from datetime import datetime
start = datetime.now()
# your code
end = datetime.now()
time_taken = end - start
print('Time: ',time_taken)
I was looking for a way how to output a formatted time with minimal code, so here is my solution. Many people use Pandas anyway, so in some cases this can save from additional library imports.
import pandas as pd
start = pd.Timestamp.now()
# code
print(pd.Timestamp.now()-start)
Output:
0 days 00:05:32.541600
I would recommend using this if time precision is not the most important, otherwise use time library:
%timeit pd.Timestamp.now() outputs 3.29 µs ± 214 ns per loop
%timeit time.time() outputs 154 ns ± 13.3 ns per loop
Putting the code in a function, then using a decorator for timing is another option. (Source) The advantage of this method is that you define timer once and use it with a simple additional line for every function.
First, define timer decorator:
import functools
import time
def timer(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
value = func(*args, **kwargs)
end_time = time.perf_counter()
run_time = end_time - start_time
print("Finished {} in {} secs".format(repr(func.__name__), round(run_time, 3)))
return value
return wrapper
Then, use the decorator while defining the function:
@timer
def doubled_and_add(num):
res = sum([i*2 for i in range(num)])
print("Result : {}".format(res))
Let’s try:
doubled_and_add(100000)
doubled_and_add(1000000)
Output:
Result : 9999900000
Finished 'doubled_and_add' in 0.0119 secs
Result : 999999000000
Finished 'doubled_and_add' in 0.0897 secs
Note: I’m not sure why to use time.perf_counter instead of time.time. Comments are welcome.
回答 6
您也可以尝试以下方法:
from time import perf_counter
t0 = perf_counter()
...
t1 = perf_counter()
time_taken = t1 - t0
What I want is to start counting time somewhere in my code and then get the passed time, to measure the time it took to execute few function. I think I’m using the timeit module wrong, but the docs are just confusing for me.
If you just want to measure the elapsed wall-clock time between two points, you could use time.time():
import time
start = time.time()
print("hello")
end = time.time()
print(end - start)
This gives the execution time in seconds.
Another option since 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() or process_time() instead,
depending on your requirements, to have a well defined behaviour.
with elapsed_timer()as elapsed:# some lengthy codeprint("midpoint at %.2f seconds"% elapsed())# time so far# other lengthy codeprint("all done at %.2f seconds"% elapsed())
It’s fun to do this with a context-manager that automatically remembers the start time upon entry to a with block, then freezes the end time on block exit. With a little trickery, you can even get a running elapsed-time tally inside the block from the same context-manager function.
The core library doesn’t have this (but probably ought to). Once in place, you can do things like:
with elapsed_timer() as elapsed:
# some lengthy code
print( "midpoint at %.2f seconds" % elapsed() ) # time so far
# other lengthy code
print( "all done at %.2f seconds" % elapsed() )
from contextlib import contextmanager
from timeit import default_timer
@contextmanager
def elapsed_timer():
start = default_timer()
elapser = lambda: default_timer() - start
yield lambda: elapser()
end = default_timer()
elapser = lambda: end-start
And some runnable demo code:
import time
with elapsed_timer() as elapsed:
time.sleep(1)
print(elapsed())
time.sleep(2)
print(elapsed())
time.sleep(3)
Note that by design of this function, the return value of elapsed() is frozen on block exit, and further calls return the same duration (of about 6 seconds in this toy example).
回答 5
以秒为单位的测量时间:
from timeit import default_timer as timer
from datetime import timedelta
start = timer()
end = timer()print(timedelta(seconds=end-start))
from datetime import datetime
start_time = datetime.now()
# INSERT YOUR CODE
time_elapsed = datetime.now() - start_time
print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
Note, that there isn’t any formatting going on here, I just wrote hh:mm:ss into the printout so one can interpret time_elapsed
回答 7
这是执行此操作的另一种方法:
>>from pytictoc importTicToc>> t =TicToc()# create TicToc instance>> t.tic()# Start timer>># do something>> t.toc()# Print elapsed timeElapsed time is2.612231 seconds.
与传统方式比较:
>>from time import time
>> t1 = time()>># do something>> t2 = time()>> elapsed = t2 - t1
>>print('Elapsed time is %f seconds.'% elapsed)Elapsed time is2.612231 seconds.
>> from pytictoc import TicToc
>> t = TicToc() # create TicToc instance
>> t.tic() # Start timer
>> # do something
>> t.toc() # Print elapsed time
Elapsed time is 2.612231 seconds.
Comparing with traditional way:
>> from time import time
>> t1 = time()
>> # do something
>> t2 = time()
>> elapsed = t2 - t1
>> print('Elapsed time is %f seconds.' % elapsed)
Elapsed time is 2.612231 seconds.
Here are my findings after going through many good answers here as well as a few other articles.
First, if you are debating between timeit and time.time, the timeit has two advantages:
timeit selects the best timer available on your OS and Python version.
timeit disables garbage collection, however, this is not something you may or may not want.
Now the problem is that timeit is not that simple to use because it needs setup and things get ugly when you have a bunch of imports. Ideally, you just want a decorator or use with block and measure time. Unfortunately, there is nothing built-in available for this so you have two options:
Option 1: Use timebudget library
The timebudget is a versatile and very simple library that you can use just in one line of code after pip install.
@timebudget # Record how long this function takes
def my_method():
# my code
Option 2: Use code module directly
I created below little utility module.
# utils.py
from functools import wraps
import gc
import timeit
def MeasureTime(f, no_print=False, disable_gc=False):
@wraps(f)
def _wrapper(*args, **kwargs):
gcold = gc.isenabled()
if disable_gc:
gc.disable()
start_time = timeit.default_timer()
try:
result = f(*args, **kwargs)
finally:
elapsed = timeit.default_timer() - start_time
if disable_gc and gcold:
gc.enable()
if not no_print:
print('"{}": {}s'.format(f.__name__, elapsed))
return result
return _wrapper
class MeasureBlockTime:
def __init__(self,name="(block)", no_print=False, disable_gc=False):
self.name = name
self.no_print = no_print
self.disable_gc = disable_gc
def __enter__(self):
self.gcold = gc.isenabled()
if self.disable_gc:
gc.disable()
self.start_time = timeit.default_timer()
def __exit__(self,ty,val,tb):
self.elapsed = timeit.default_timer() - self.start_time
if self.disable_gc and self.gcold:
gc.enable()
if not self.no_print:
print('Function "{}": {}s'.format(self.name, self.elapsed))
return False #re-raise any exceptions
Now you can time any function just by putting a decorator in front of it:
import utils
@utils.MeasureTime
def MyBigFunc():
#do something time consuming
for i in range(10000):
print(i)
If you want to time portion of code then just put it inside with block:
import utils
#somewhere in my code
with utils.MeasureBlockTime("MyBlock"):
#do something time consuming
for i in range(10000):
print(i)
# rest of my code
Advantages:
There are several half-backed versions floating around so I want to point out few highlights:
Use timer from timeit instead of time.time for reasons described earlier.
You can disable GC during timing if you want.
Decorator accepts functions with named or unnamed params.
Ability to disable printing in block timing (use with utils.MeasureBlockTime() as t and then t.elapsed).
Using time.time to measure execution gives you the overall execution time of your commands including running time spent by other processes on your computer. It is the time the user notices, but is not good if you want to compare different code snippets / algorithms / functions / …
Update: I used http://pythonhosted.org/line_profiler/ a lot during the last year and find it very helpfull and recommend to use it instead of Pythons profile module.
回答 10
这是一个微小的计时器类,它返回“ hh:mm:ss”字符串:
classTimer:def __init__(self):
self.start = time.time()def restart(self):
self.start = time.time()def get_time_hhmmss(self):
end = time.time()
m, s = divmod(end - self.start,60)
h, m = divmod(m,60)
time_str ="%02d:%02d:%02d"%(h, m, s)return time_str
用法:
# Start timer
my_timer =Timer()# ... do something# Get time string:
time_hhmmss = my_timer.get_time_hhmmss()print("Time elapsed: %s"% time_hhmmss )# ... use the timer again
my_timer.restart()# ... do something# Get time:
time_hhmmss = my_timer.get_time_hhmmss()# ... etc
Welcome to the profile statistics browser.
timeStats.profile% stats hello
<timestamp> timeStats.profile
224 function calls in6.014 seconds
Random listing order was used
List reduced from6 to 1 due to restriction <'hello'>
ncalls tottime percall cumtime percall filename:lineno(function)100.0000.0001.0010.100 timeFunctions.py:3(hello)
timeStats.profile% stats thankyou
<timestamp> timeStats.profile
224 function calls in6.014 seconds
Random listing order was used
List reduced from6 to 1 due to restriction <'thankyou'>
ncalls tottime percall cumtime percall filename:lineno(function)1000.0020.0005.0120.050 timeFunctions.py:7(thankyou)
The python cProfile and pstats modules offer great support for measuring time elapsed in certain functions without having to add any code around the existing functions.
For example if you have a python script timeFunctions.py:
import time
def hello():
print "Hello :)"
time.sleep(0.1)
def thankyou():
print "Thank you!"
time.sleep(0.05)
for idx in range(10):
hello()
for idx in range(100):
thankyou()
To run the profiler and generate stats for the file you can just run:
What this is doing is using the cProfile module to profile all functions in timeFunctions.py and collecting the stats in the timeStats.profile file. Note that we did not have to add any code to existing module (timeFunctions.py) and this can be done with any module.
Once you have the stats file you can run the pstats module as follows:
python -m pstats timeStats.profile
This runs the interactive statistics browser which gives you a lot of nice functionality. For your particular use case you can just check the stats for your function. In our example checking stats for both functions shows us the following:
Welcome to the profile statistics browser.
timeStats.profile% stats hello
<timestamp> timeStats.profile
224 function calls in 6.014 seconds
Random listing order was used
List reduced from 6 to 1 due to restriction <'hello'>
ncalls tottime percall cumtime percall filename:lineno(function)
10 0.000 0.000 1.001 0.100 timeFunctions.py:3(hello)
timeStats.profile% stats thankyou
<timestamp> timeStats.profile
224 function calls in 6.014 seconds
Random listing order was used
List reduced from 6 to 1 due to restriction <'thankyou'>
ncalls tottime percall cumtime percall filename:lineno(function)
100 0.002 0.000 5.012 0.050 timeFunctions.py:7(thankyou)
The dummy example does not do much but give you an idea of what can be done. The best part about this approach is that I dont have to edit any of my existing code to get these numbers and obviously help with profiling.
回答 12
这是另一个用于计时代码的上下文管理器-
用法:
from benchmark import benchmark
with benchmark("Test 1+1"):1+1=>Test1+1:1.41e-06 seconds
或者,如果您需要时间值
with benchmark("Test 1+1")as b:1+1print(b.time)=>Test1+1:7.05e-07 seconds
7.05233786763e-07
Benchmark.py:
from timeit import default_timer as timer
class benchmark(object):def __init__(self, msg, fmt="%0.3g"):
self.msg = msg
self.fmt = fmt
def __enter__(self):
self.start = timer()return self
def __exit__(self,*args):
t = timer()- self.start
print(("%s : "+ self.fmt +" seconds")%(self.msg, t))
self.time = t
from timeit import timeit
timeit(lambda: print("hello"))
Output is microseconds for a single execution:
2.430883963010274
Explanation:
timeit executes the anonymous function 1 million times by default and the result is given in seconds. Therefore the result for 1 single execution is the same amount but in microseconds on average.
For slow operations add a lower number of iterations or you could be waiting forever:
import time
timeit(lambda: time.sleep(1.5), number=1)
Output is always in seconds for the total number of iterations:
from time import sleep, perf_counter as pc
t0 = pc()
sleep(1)
print(pc()-t0)
elegant and short.
回答 18
有点超级后来的反应,但也许对某人有用。我认为这是一种超级干净的方法。
import time
def timed(fun,*args):
s = time.time()
r = fun(*args)print('{} execution took {} seconds.'.format(fun.__name__, time.time()-s))return(r)
timed(print,"Hello")
Kind of a super later response, but maybe it serves a purpose for someone. This is a way to do it which I think is super clean.
import time
def timed(fun, *args):
s = time.time()
r = fun(*args)
print('{} execution took {} seconds.'.format(fun.__name__, time.time()-s))
return(r)
timed(print, "Hello")
Keep in mind that “print” is a function in Python 3 and not Python 2.7. However, it works with any other function. Cheers!
回答 19
您可以使用timeit。
这是一个有关如何使用Python REPL测试带参数的naive_func的示例:
>>>import timeit
>>>def naive_func(x):... a =0...for i in range(a):... a += i
...return a
>>>def wrapper(func,*args,**kwargs):...def wrapper():...return func(*args,**kwargs)...return wrapper
>>> wrapped = wrapper(naive_func,1_000)>>> timeit.timeit(wrapped, number=1_000_000)0.4458435332577161
Here is an example on how to test naive_func that takes parameter using Python REPL:
>>> import timeit
>>> def naive_func(x):
... a = 0
... for i in range(a):
... a += i
... return a
>>> def wrapper(func, *args, **kwargs):
... def wrapper():
... return func(*args, **kwargs)
... return wrapper
>>> wrapped = wrapper(naive_func, 1_000)
>>> timeit.timeit(wrapped, number=1_000_000)
0.4458435332577161
You don’t need wrapper function if function doesn’t have any parameters.
回答 20
我们还可以将时间转换为人类可以理解的时间。
import time, datetime
start = time.clock()def num_multi1(max):
result =0for num in range(0,1000):if(num %3==0or num %5==0):
result += num
print"Sum is %d "% result
num_multi1(1000)
end = time.clock()
value = end - start
timestamp = datetime.datetime.fromtimestamp(value)print timestamp.strftime('%Y-%m-%d %H:%M:%S')
We can also convert time into human-readable time.
import time, datetime
start = time.clock()
def num_multi1(max):
result = 0
for num in range(0, 1000):
if (num % 3 == 0 or num % 5 == 0):
result += num
print "Sum is %d " % result
num_multi1(1000)
end = time.clock()
value = end - start
timestamp = datetime.datetime.fromtimestamp(value)
print timestamp.strftime('%Y-%m-%d %H:%M:%S')
回答 21
我为此创建了一个库,如果您想测量一个函数,可以像这样做
from pythonbenchmark import compare, measure
import time
a,b,c,d,e =10,10,10,10,10
something =[a,b,c,d,e]@measuredef myFunction(something):
time.sleep(0.4)@measuredef myOptimizedFunction(something):
time.sleep(0.2)
myFunction(input)
myOptimizedFunction(input)
To get insight on every function calls recursively, do:
%load_ext snakeviz
%%snakeviz
It just takes those 2 lines of code in a Jupyter notebook, and it generates a nice interactive diagram. For example:
Here is the code. Again, the 2 lines starting with % are the only extra lines of code needed to use snakeviz:
# !pip install snakeviz
%load_ext snakeviz
import glob
import hashlib
%%snakeviz
files = glob.glob('*.txt')
def print_files_hashed(files):
for file in files:
with open(file) as f:
print(hashlib.md5(f.read().encode('utf-8')).hexdigest())
print_files_hashed(files)
It also seems possible to run snakeviz outside notebooks. More info on the snakeviz website.
回答 23
import time
def getElapsedTime(startTime, units):
elapsedInSeconds = time.time()- startTime
if units =='sec':return elapsedInSeconds
if units =='min':return elapsedInSeconds/60if units =='hour':return elapsedInSeconds/(60*60)
import time
def getElapsedTime(startTime, units):
elapsedInSeconds = time.time() - startTime
if units == 'sec':
return elapsedInSeconds
if units == 'min':
return elapsedInSeconds/60
if units == 'hour':
return elapsedInSeconds/(60*60)
# Setup timer>>> timer =Timer()# Access as a string>>>print(f'Time elapsed is {timer}.')Time elapsed is0:00:03.>>>print(f'Time elapsed is {timer}.')Time elapsed is0:00:04.# Access as a float>>> timer()6.841332235>>> timer()7.970274425
This unique class-based approach offers a printable string representation, customizable rounding, and convenient access to the elapsed time as a string or a float. It was developed with Python 3.7.
# Setup timer
>>> timer = Timer()
# Access as a string
>>> print(f'Time elapsed is {timer}.')
Time elapsed is 0:00:03.
>>> print(f'Time elapsed is {timer}.')
Time elapsed is 0:00:04.
# Access as a float
>>> timer()
6.841332235
>>> timer()
7.970274425
回答 25
测量小代码段的执行时间。
时间单位:以秒为单位,以浮点数表示
import timeit
t = timeit.Timer('li = list(map(lambda x:x*2,[1,2,3,4,5]))')
t.timeit()
t.repeat()>[1.2934070999999676,1.3335035000000062,1.422568500000125]
The repeat() method is a convenience to call timeit() multiple times and return a list of results.
repeat(repeat=3)¶
With this list we can take a mean of all times.
By default, timeit() temporarily turns off garbage collection during the timing. time.Timer() solves this problem.
Pros:
timeit.Timer() makes independent timings more comparable. The gc may be an important component of the performance of the function being measured. If so, gc(garbage collector) can be re-enabled as the first statement in the setup string. For example:
import time
start = time.time()
sleep(5) #just to give it some delay to show it working
finish = time.time()
elapsed = finish - start
print(elapsed)
Hope that will help.
回答 29
该timeit模块非常适合计时一小段Python代码。它至少可以以三种形式使用:
1-作为命令行模块
python2 -m timeit 'for i in xrange(10): oct(i)'
2-对于短代码,请将其作为参数传递。
import timeit
timeit.Timer('for i in xrange(10): oct(i)').timeit()
3-对于更长的代码为:
import timeit
code_to_test ="""
a = range(100000)
b = []
for i in a:
b.append(i*2)
"""
elapsed_time = timeit.timeit(code_to_test, number=100)/100print(elapsed_time)
The timeit module is good for timing a small piece of Python code. It can be used at least in three forms:
1- As a command-line module
python2 -m timeit 'for i in xrange(10): oct(i)'
2- For a short code, pass it as arguments.
import timeit
timeit.Timer('for i in xrange(10): oct(i)').timeit()
3- For longer code as:
import timeit
code_to_test = """
a = range(100000)
b = []
for i in a:
b.append(i*2)
"""
elapsed_time = timeit.timeit(code_to_test, number=100)/100
print(elapsed_time)