欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python函數(shù)運(yùn)行內(nèi)存時(shí)間等性能檢測(cè)工具

 更新時(shí)間:2022年05月12日 11:47:01   作者:Sir?老王  
這篇文章主要為大家介紹了python函數(shù)運(yùn)行內(nèi)存時(shí)間等性能檢測(cè)工具,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

python雖然是一門(mén)'慢語(yǔ)言',但是也有著比較多的性能檢測(cè)工具來(lái)幫助我們優(yōu)化程序的運(yùn)行效率。

這里總結(jié)了五個(gè)比較好的python性能檢測(cè)工具,包括內(nèi)存使用、運(yùn)行時(shí)間、執(zhí)行次數(shù)等方面。

基礎(chǔ)測(cè)試函數(shù)

首先,來(lái)編寫(xiě)一個(gè)基礎(chǔ)的python函數(shù)用于在后面的各種性能測(cè)試。

def?base_func():
????for?n?in?range(10000):
????????print('當(dāng)前n的值是:{}'.format(n))

memory_profiler進(jìn)程

memory_profiler是python的非標(biāo)準(zhǔn)庫(kù),所以這里采用pip的方式進(jìn)行安裝。它能夠監(jiān)視進(jìn)程、了解內(nèi)存使用等情況。

pip?install?memory_profiler

安裝好memory_profiler庫(kù)以后,直接使用注解的方式進(jìn)行測(cè)試。

from?memory_profiler?import?profile
@profile
def?base_func1():
????for?n?in?range(10000):
????????print('當(dāng)前n的值是:{}'.format(n))
base_func1()
#?Line?#????Mem?usage????Increment??Occurrences???Line?Contents
#?=============================================================
#?????28?????45.3?MiB?????45.3?MiB???????????1???@profile
#?????29?????????????????????????????????????????def?base_func():
#?????30?????45.3?MiB??????0.0?MiB???????10001???????for?n?in?range(10000):
#???? 31 ??? 45.3 MiB ?????0.0 MiB ????? 10000?????????? print('當(dāng)前n的值是:{}'.format(n))

從返回的數(shù)據(jù)結(jié)果來(lái)看,執(zhí)行當(dāng)前函數(shù)使用了45.3 MiB的內(nèi)存。

timeit 時(shí)間使用情況

timeit是python的內(nèi)置模塊,可以測(cè)試單元格的代碼運(yùn)行時(shí)間,由于是內(nèi)置模塊所以并不需要單獨(dú)安裝。

import?timeit
def?base_func2():
????for?n?in?range(10000):
????????print('當(dāng)前n的值是:{}'.format(n))
res?=?timeit.timeit(base_func2,number=5)
print('當(dāng)前的函數(shù)的運(yùn)行時(shí)間是:{}'.format(res))
#?當(dāng)前的函數(shù)的運(yùn)行時(shí)間是:0.9675800999999993

根據(jù)上面函數(shù)的運(yùn)行返回結(jié)果,函數(shù)的運(yùn)行時(shí)間是0.96秒。

line_profiler行代碼檢測(cè)

如果在只需要檢測(cè)函數(shù)的局部運(yùn)行時(shí)間的話就可以使用line_profiler了,它可以檢測(cè)出每行代碼的運(yùn)行時(shí)間。

line_profiler是python的非標(biāo)準(zhǔn)庫(kù),使用的使用pip的方式安裝一下。

pip?install?line_profiler

最簡(jiǎn)便的使用方式直接將需要測(cè)試的函數(shù)加入即可。

def?base_func3():
????for?n?in?range(10000):
????????print('當(dāng)前n的值是:{}'.format(n))
from?line_profiler?import?LineProfiler
lp?=?LineProfiler()
lp_wrap?=?lp(base_func3)
lp_wrap()
lp.print_stats()
#?Line?#??????Hits?????????Time??Per?Hit???%?Time??Line?Contents
#?==============================================================
#?????72???????????????????????????????????????????def?base_func3():
#?????73?????10001?????162738.0?????16.3??????4.8??????for?n?in?range(10000):
#???? 74 ??? 10000??? 3207772.0??? 320.8 ??? 95.2 ???????? print('當(dāng)前n的值是:{}'.format(n))

從運(yùn)行結(jié)果可以看出每行代碼的運(yùn)行時(shí)間及比例,注意這里的時(shí)間單位是微妙。

heartrate可視化檢測(cè)

heartrate最值得推薦的是可以在網(wǎng)頁(yè)上面向檢測(cè)心率一樣檢測(cè)程序的執(zhí)行過(guò)程,同時(shí),他還是非標(biāo)準(zhǔn)庫(kù),使用pip的方式進(jìn)行安裝。

pip?install?heartrate
import?heartrate
heartrate.trace(browser=True)
def?base_func4():
????for?n?in?range(10000):
????????print('當(dāng)前n的值是:{}'.format(n))

運(yùn)行以后,控制臺(tái)打印如下日志:

#??*?Serving?Flask?app?"heartrate.core"?(lazy?loading)
#??*?Environment:?production
#????WARNING:?This?is?a?development?server.?Do?not?use?it?in?a?production?deployment.
#????Use?a?production?WSGI?server?instead.
#??*?Debug?mode:?off

并且自動(dòng)打開(kāi)瀏覽器地址:http://127.0.0.1:9999

以上就是python函數(shù)運(yùn)行內(nèi)存時(shí)間等性能檢測(cè)工具的詳細(xì)內(nèi)容,更多關(guān)于python性能檢測(cè)工具的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論