Python中計(jì)算函數(shù)執(zhí)行時(shí)間的五種方法
1. time.time()
在計(jì)算函數(shù)執(zhí)行時(shí)間時(shí),這種時(shí)最簡潔的一種方式,用兩個(gè)時(shí)間戳做減法。
import time
def func():
print('func start')
time.sleep(1)
print('func end')
t = time.time()
func()
print(f'coast:{time.time() - t:.4f}s')
結(jié)果為:
func start
func end
coast:1.0003s
這種方法很簡單,也很常用,但是如果想更精確的計(jì)算函數(shù)的執(zhí)行時(shí)間,就會(huì)產(chǎn)生精度缺失。
2. time.perf_counter() 推薦
示例一中注釋掉time.sleep(1),只統(tǒng)計(jì)兩個(gè) print 函數(shù)的執(zhí)行時(shí)間:
import time
def func():
print('func start')
# time.sleep(1)
print('func end')
t = time.time()
func()
print(f'coast:{time.time() - t:.8f}s')輸出:
func start
func end
coast:0.0000s
這就說明time.time() 函數(shù)的精度不是特別高,沒法統(tǒng)計(jì)執(zhí)行時(shí)間極短的函數(shù)耗時(shí)。
perf_counter 函數(shù)是在 python3.3 中新添加的,它返回性能計(jì)數(shù)器的值,返回值是浮點(diǎn)型,統(tǒng)計(jì)結(jié)果包括睡眠的時(shí)間,單個(gè)函數(shù)的返回值無意義,只有多次運(yùn)行取差值的結(jié)果才是有效的函數(shù)執(zhí)行時(shí)間。
import time
def func():
print('func start')
# time.sleep(1)
print('func end')
t = time.perf_counter()
func()
print(f'coast:{time.perf_counter() - t:.8f}s')結(jié)果
func start
func end
coast:0.00001500s
結(jié)果并不是 0 秒, 而是一個(gè)很小的值,這說明 perf_counter() 函數(shù)可以統(tǒng)計(jì)出 print 函數(shù)的執(zhí)行耗時(shí),并且統(tǒng)計(jì)精度要比 time.time() 函數(shù)要高,比較推薦作為計(jì)時(shí)器來使用。
3. timeit.timeit ()
timeit() 函數(shù)有 5 個(gè)參數(shù),stmt=‘pass’, setup=‘pass’, timer=, number=1000000, globals=None。
- stmt 參數(shù)是需要執(zhí)行的語句,默認(rèn)為 pass
- setup 參數(shù)是用來執(zhí)行初始化代碼或構(gòu)建環(huán)境的語句,默認(rèn)為 pass
- timer 是計(jì)時(shí)器,默認(rèn)是 perf_counter()
- number 是執(zhí)行次數(shù),默認(rèn)為一百萬
- globals 用來指定要運(yùn)行代碼的命名空間,默認(rèn)為 None。
import time
import timeit
def func():
print('func start')
time.sleep(1)
print('func end')
print(timeit.timeit(stmt=func, number=1))
以上方案中比較推薦使用的是 time.perf_counter() 函數(shù),它具有比較高的精度,同時(shí)還被用作 timeit 函數(shù)默認(rèn)的計(jì)時(shí)器。
4.裝飾器統(tǒng)計(jì)運(yùn)行耗時(shí)
在實(shí)際項(xiàng)目代碼中,可以通過裝飾器方便的統(tǒng)計(jì)函數(shù)運(yùn)行耗時(shí)。
import time
def cost_time(func):
def fun(*args, **kwargs):
t = time.perf_counter()
result = func(*args, **kwargs)
print(f'func {func.__name__} cost time:{time.perf_counter() - t:.8f} s')
return result
return fun
@cost_time
def test():
print('func start')
time.sleep(2)
print('func end')
if __name__ == '__main__':
test()
如果有使用異步函數(shù)的需求也可以加上:
import asyncio
import time
from asyncio.coroutines import iscoroutinefunction
def cost_time(func):
def fun(*args, **kwargs):
t = time.perf_counter()
result = func(*args, **kwargs)
print(f'func {func.__name__} cost time:{time.perf_counter() - t:.8f} s')
return result
async def func_async(*args, **kwargs):
t = time.perf_counter()
result = await func(*args, **kwargs)
print(f'func {func.__name__} cost time:{time.perf_counter() - t:.8f} s')
return result
if iscoroutinefunction(func):
return func_async
else:
return fun
@cost_time
def test():
print('func start')
time.sleep(2)
print('func end')
@cost_time
async def test_async():
print('async func start')
await asyncio.sleep(2)
print('async func end')
if __name__ == '__main__':
test()
asyncio.get_event_loop().run_until_complete(test_async())
使用裝飾器來統(tǒng)計(jì)函數(shù)執(zhí)行耗時(shí)的好處是對函數(shù)的入侵性小,易于編寫和修改。
裝飾器裝飾函數(shù)的方案只適用于統(tǒng)計(jì)函數(shù)的運(yùn)行耗時(shí),如果有代碼塊耗時(shí)統(tǒng)計(jì)的需求就不能用了,這種情況下我們可以使用 with 語句自動(dòng)管理上下文。
5. with 語句統(tǒng)計(jì)運(yùn)行耗時(shí)
通過實(shí)現(xiàn) enter 和 exit 函數(shù)可以讓我們在進(jìn)入上下文和退出上下文時(shí)進(jìn)行一些自定義動(dòng)作,例如連接 / 斷開數(shù)據(jù)庫、打開 / 關(guān)閉文件、記錄開始 / 結(jié)束時(shí)間等等,這里我們用來統(tǒng)計(jì)函數(shù)塊的執(zhí)行時(shí)間。
import asyncio
import time
class CostTime(object):
def __init__(self):
self.t = 0
def __enter__(self):
self.t = time.perf_counter()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print(f'cost time:{time.perf_counter() - self.t:.8f} s')
def test():
print('func start')
with CostTime():
time.sleep(2)
print('func end')
async def test_async():
print('async func start')
with CostTime():
await asyncio.sleep(2)
print('async func end')
if __name__ == '__main__':
test()
asyncio.get_event_loop().run_until_complete(test_async())
with 語句不僅可以統(tǒng)計(jì)代碼塊的執(zhí)行時(shí)間,也可以統(tǒng)計(jì)函數(shù)的執(zhí)行時(shí)間,還可以統(tǒng)計(jì)多個(gè)函數(shù)的執(zhí)行時(shí)間之和,相比裝飾器來說對代碼的入侵性比較大,不易于修改,好處是使用起來比較靈活,不用寫過多的重復(fù)代碼。
6.延展:python實(shí)現(xiàn)函數(shù)超時(shí)退出
方法一
import time
import eventlet#導(dǎo)入eventlet這個(gè)模塊
eventlet.monkey_patch()#必須加這條代碼
with eventlet.Timeout(5,False):#設(shè)置超時(shí)時(shí)間為2秒
time.sleep(4)
print('沒有跳過這條輸出')
print('跳過了輸出')
第二個(gè)方法 不太會(huì)用,用的不成功,不如第一個(gè)
import time
import timeout_decorator
@timeout_decorator.timeout(5)
def mytest():
print("Start")
for i in range(1,10):
time.sleep(1)
print("{} seconds have passed".format(i))
if __name__ == '__main__':
mytest()
Python設(shè)置函數(shù)調(diào)用超時(shí)
import time import signal def test(i): time.sleep(i%4) print "%d within time"%(i) return i if __name__ == '__main__': def handler(signum, frame): raise AssertionError i = 0 for i in range(1,10): try: signal.signal(signal.SIGALRM, handler) signal.alarm(3) test(i) i = i + 1 signal.alarm(0) except AssertionError: print "%d timeout"%(i)
說明:
1、調(diào)用test函數(shù)超時(shí)監(jiān)控,使用sleep模擬函數(shù)執(zhí)行超時(shí)
2、引入signal模塊,設(shè)置handler捕獲超時(shí)信息,返回?cái)嘌藻e(cuò)誤
3、alarm(3),設(shè)置3秒鬧鐘,函數(shù)調(diào)用超時(shí)3秒則直接返回
4、捕獲異常,打印超時(shí)信息
執(zhí)行結(jié)果
within time
within time
timeout
within time
within time
within time
timeout
within time
within time
到此這篇關(guān)于Python中計(jì)算函數(shù)執(zhí)行時(shí)間的五種方法的文章就介紹到這了,更多相關(guān)Python計(jì)算函數(shù)執(zhí)行時(shí)間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)WebSocket服務(wù)端過程解析
這篇文章主要介紹了python實(shí)現(xiàn)WebSocket服務(wù)端過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Python學(xué)習(xí)筆記之變量與轉(zhuǎn)義符
這篇文章主要介紹了Python學(xué)習(xí)筆記之變量與轉(zhuǎn)義符,本文從零開始學(xué)習(xí)Python,知識點(diǎn)很細(xì),有共同目標(biāo)的小伙伴可以一起來學(xué)習(xí)2023-03-03
如何解決django配置settings時(shí)遇到Could not import settings ''conf.loca
這里記錄一下在項(xiàng)目中遇到django配置settings時(shí)遇到Could not import settings 'conf.local'的解決方法,有同樣問題的小伙伴們參考下吧2014-11-11
深入理解Python虛擬機(jī)中描述器的實(shí)現(xiàn)原理
這篇文章主要給大家介紹一個(gè)我們在使用類的時(shí)候經(jīng)常使用但是卻很少在意的黑科技——描述器的實(shí)現(xiàn)原理,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-05-05
Python在畫圖時(shí)使用特殊符號的方法總結(jié)
在制作圖表時(shí),如果遇到需要利用特殊符號進(jìn)行表示時(shí)該怎么辦呢?不用慌,這篇文章為大家總結(jié)了python畫圖中使用各種特殊符號的方式,需要的可以參考一下2022-04-04
Django實(shí)現(xiàn)發(fā)送郵件找回密碼功能
在各大網(wǎng)站上,一定都遇到過找回密碼的問題,通常采用的方式是通過發(fā)送帶有驗(yàn)證碼的郵件進(jìn)行身份驗(yàn)證,本文將介紹通過Django實(shí)現(xiàn)郵件找回密碼功能,需要的朋友可以參考下2019-08-08

