4 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
一、benchrun.py
import datetime
print ("sleeping now...")
time.sleep(10)
print ("done!")
包裝:
import os
from datetime import datetime, timedelta
before = datetime.now()
os.system("python benchrun.py")
after = datetime.now()
print ("execution time: {0}".format(after - before))

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊
# timer.py
import time
def timer():
def wrapper(f):
def wrapped_f(*args, **kwargs):
tic = time.perf_counter() # more precise than '.clock'
f(*args, **kwargs)
toc = time.perf_counter()
method_name = f.__name__
print('{}: {:.2f}sec'.format(method_name, toc - tic))
return wrapped_f
return wrapper
# benchrun.py
from timer import timer
@timer
def benchrun():
...

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊
您需要在執(zhí)行前后計(jì)算時(shí)間并減少它們之間的時(shí)間,以便獲得執(zhí)行時(shí)間;
在包裝器腳本中:
import time
執(zhí)行前:
before= time.clock()
執(zhí)行后:
after = time.clock() totaltime = after-before
totaltime 是 benchrun.py 腳本的執(zhí)行時(shí)間。

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊
你在使用 os.system 來(lái)調(diào)用 benchrun.py 嗎?如果是這樣,只需在開(kāi)始和返回后設(shè)置 datetime.now() ,并計(jì)算增量。它應(yīng)該工作。
添加回答
舉報(bào)