有没有一种方法可以查看脚本在VS代码中执行/完成所用的时间?
我在找这样的信息:
Program finished in 30ms发布于 2019-08-08 09:34:50
利用‘时间’
当脚本开始时:
import time
start_time = time.time()
do something # here your actual code/routine
print("Process finished --- %s seconds ---" % (time.time() - start_time))发布于 2019-08-08 10:26:42
您可以创建一个简单的装饰函数来计时您的函数。
import time
def decoratortimer(decimal):
def decoratorfunction(f):
def wrap(*args, **kwargs):
time1 = time.monotonic()
result = f(*args, **kwargs)
time2 = time.monotonic()
print('{:s} function took {:.{}f} ms'.format(f.__name__, ((time2-time1)*1000.0), decimal ))
return result
return wrap
return decoratorfunction
@decoratortimer(2)
def callablefunction(name):
print(name)
print(callablefunction('John'))我建议使用time.monotonic(它是一个不会倒退的时钟)来提高精确度。
发布于 2019-08-08 09:33:43
要实现这一点,最简单的方法是单纯地编码编程时间。perf_counter提供了time函数的最高精度。
from time import perf_counter, sleep
def main():
sleep(5)
start_time = perf_counter()
main() # Function to measure
passed_time = perf_counter() - start_time
print(f"It took {passed_time}") # It took 5.007398507999824https://stackoverflow.com/questions/57409385
复制相似问题