Windows上Python中的计时器
如果我有一个函数在for循环中多次调用,并且这个函数有时运行的时间太长,那么我如何为每次调用函数使用计时器(每次设置并重置计时器) 它看起来像:Windows上Python中的计时器,python,windows,Python,Windows,如果我有一个函数在for循环中多次调用,并且这个函数有时运行的时间太长,那么我如何为每次调用函数使用计时器(每次设置并重置计时器) 它看起来像: def theFunction(*args): #some code (timer is on) #In this point time is out, break and exit function #Timer is reseted for i in range(0,100): theFunction(*
def theFunction(*args):
#some code (timer is on)
#In this point time is out, break and exit function
#Timer is reseted
for i in range(0,100):
theFunction(*args)
使用
时间
模块,如下所示:
import time
time_start = time.time()
#Do function stuff
time_stop = time.time()
#Check your time now
timed_segment = time_stop - time_start
#Repeat if needed
要在for循环中多次运行此操作,您需要在列表中添加时间,因为它是这样运行的:
import time
def function():
times_list = []
for x in range(10)
time_start = time.time()
#Do function stuff
time_stop = time.time()
#Check your time now
timed_segment = time_stop - time_start
times_list.append(timed_segment)
#Repeat as many times as needed
return times_list
import threading
from time import sleep
def timeout(func):
def inner_func(*nums, **kwargs):
t = threading.Thread(target=func, args=(*nums,))
t.start()
t.join(timeout=5)
return inner_func
@timeout
def do_stuff(a,b):
sleep(3)
print(a+b)
return
do_stuff(1,3)
如果要在一定时间后中断
,可以使用while
循环,如下所示:
import time
def function():
times_list = []
time_start = time.time()
time_end = time.time()
while time_end - time_start < 10: #after 10 seconds the while loop will time out
#Your function does stuff here
time_end = time.time()
#Next, append times to a list if needed
time_list.append(time_start - time_end)
return times_list
在上面的示例中,在join
中调用timeout
将在5秒后终止线程。如果我们计划多次重复使用它,我们也可以将其放入装饰器中:
import time
def function():
times_list = []
for x in range(10)
time_start = time.time()
#Do function stuff
time_stop = time.time()
#Check your time now
timed_segment = time_stop - time_start
times_list.append(timed_segment)
#Repeat as many times as needed
return times_list
import threading
from time import sleep
def timeout(func):
def inner_func(*nums, **kwargs):
t = threading.Thread(target=func, args=(*nums,))
t.start()
t.join(timeout=5)
return inner_func
@timeout
def do_stuff(a,b):
sleep(3)
print(a+b)
return
do_stuff(1,3)
使用
时间
模块,如下所示:
import time
time_start = time.time()
#Do function stuff
time_stop = time.time()
#Check your time now
timed_segment = time_stop - time_start
#Repeat if needed
要在for循环中多次运行此操作,您需要在列表中添加时间,因为它是这样运行的:
import time
def function():
times_list = []
for x in range(10)
time_start = time.time()
#Do function stuff
time_stop = time.time()
#Check your time now
timed_segment = time_stop - time_start
times_list.append(timed_segment)
#Repeat as many times as needed
return times_list
import threading
from time import sleep
def timeout(func):
def inner_func(*nums, **kwargs):
t = threading.Thread(target=func, args=(*nums,))
t.start()
t.join(timeout=5)
return inner_func
@timeout
def do_stuff(a,b):
sleep(3)
print(a+b)
return
do_stuff(1,3)
如果要在一定时间后中断
,可以使用while
循环,如下所示:
import time
def function():
times_list = []
time_start = time.time()
time_end = time.time()
while time_end - time_start < 10: #after 10 seconds the while loop will time out
#Your function does stuff here
time_end = time.time()
#Next, append times to a list if needed
time_list.append(time_start - time_end)
return times_list
在上面的示例中,在join
中调用timeout
将在5秒后终止线程。如果我们计划多次重复使用它,我们也可以将其放入装饰器中:
import time
def function():
times_list = []
for x in range(10)
time_start = time.time()
#Do function stuff
time_stop = time.time()
#Check your time now
timed_segment = time_stop - time_start
times_list.append(timed_segment)
#Repeat as many times as needed
return times_list
import threading
from time import sleep
def timeout(func):
def inner_func(*nums, **kwargs):
t = threading.Thread(target=func, args=(*nums,))
t.start()
t.join(timeout=5)
return inner_func
@timeout
def do_stuff(a,b):
sleep(3)
print(a+b)
return
do_stuff(1,3)
还有另一个名为
timeit
的模块,它可以测量小代码段的执行时间。我相信你也可以用它。我从未使用过该模块,但它应该可以工作。这是指向文档页面的链接。看一看:
另请参见还有另一个名为
timeit
的模块,它可以测量小代码段的执行时间。我相信你也可以用它。我从未使用过该模块,但它应该可以工作。这是指向文档页面的链接。看一看:
如需有关高可重用性和易于实现的信息,请参见-
- 使用装饰器-
from time import time def time_it(func): def wrapper(*args, **kwargs): a=time() func(*args, **kwargs) print(a-time()) return wrapper @time_it def foo(s='this works'): print(s) foo()
- 使用profile.run-
- 使用装饰器-
from time import time def time_it(func): def wrapper(*args, **kwargs): a=time() func(*args, **kwargs) print(a-time()) return wrapper @time_it def foo(s='this works'): print(s) foo()
- 使用profile.run-