作者:采臣--小青在这_203 | 来源:互联网 | 2018-07-18 14:46
本文实例讲述了python定时执行指定函数的方法。分享给大家供大家参考。具体实现方法如下:
# time a function using time.time() and the a @ function decorator
# tested with Python24 vegaseat 21aug2005
import time
def print_timing(func):
def wrapper(*arg):
t1 = time.time()
res = func(*arg)
t2 = time.time()
print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
return res
return wrapper
# declare the @ decorator just before the function, invokes print_timing()
@print_timing
def getPrimeList(n):
""" returns a list of prime numbers from 2 to
prime numbers from 2 to <10,000,000 using a sieve algorithm
getPrimeList took 4750.000 ms
"""
希望本文所述对大家的Python程序设计有所帮助。