溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python函數(shù)運行內(nèi)存時間等性能檢測工具如何用

發(fā)布時間:2023-05-11 11:02:08 來源:億速云 閱讀:86 作者:iii 欄目:編程語言

這篇文章主要介紹了python函數(shù)運行內(nèi)存時間等性能檢測工具如何用的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇python函數(shù)運行內(nèi)存時間等性能檢測工具如何用文章都會有所收獲,下面我們一起來看看吧。

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

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

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

memory_profiler進程

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

pip install memory_profiler

安裝好memory_profiler庫以后,直接使用注解的方式進行測試。

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é)果來看,執(zhí)行當(dāng)前函數(shù)使用了45.3 MiB的內(nèi)存。

timeit 時間使用情況

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

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ù)的運行時間是:{}'.format(res))
# 當(dāng)前的函數(shù)的運行時間是:0.9675800999999993

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

line_profiler行代碼檢測

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

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

pip install line_profiler

最簡便的使用方式直接將需要測試的函數(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))

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

heartrate可視化檢測

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

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

運行以后,控制臺打印如下日志:

#  * 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

并且自動打開瀏覽器地址:http://127.0.0.1:9999

python函數(shù)運行內(nèi)存時間等性能檢測工具如何用

關(guān)于“python函數(shù)運行內(nèi)存時間等性能檢測工具如何用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“python函數(shù)運行內(nèi)存時間等性能檢測工具如何用”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI