網站首頁 編程語言 正文
python雖然是一門'慢語言',但是也有著比較多的性能檢測工具來幫助我們優化程序的運行效率。
這里總結了五個比較好的python性能檢測工具,包括內存使用、運行時間、執行次數等方面。
基礎測試函數
首先,來編寫一個基礎的python函數用于在后面的各種性能測試。
def?base_func():
????for?n?in?range(10000):
????????print('當前n的值是:{}'.format(n))
memory_profiler進程
memory_profiler是python的非標準庫,所以這里采用pip的方式進行安裝。它能夠監視進程、了解內存使用等情況。
pip?install?memory_profiler
安裝好memory_profiler庫以后,直接使用注解的方式進行測試。
from?memory_profiler?import?profile
@profile
def?base_func1():
????for?n?in?range(10000):
????????print('當前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('當前n的值是:{}'.format(n))
從返回的數據結果來看,執行當前函數使用了45.3 MiB的內存。
timeit 時間使用情況
timeit是python的內置模塊,可以測試單元格的代碼運行時間,由于是內置模塊所以并不需要單獨安裝。
import?timeit
def?base_func2():
????for?n?in?range(10000):
????????print('當前n的值是:{}'.format(n))
res?=?timeit.timeit(base_func2,number=5)
print('當前的函數的運行時間是:{}'.format(res))
#?當前的函數的運行時間是:0.9675800999999993
根據上面函數的運行返回結果,函數的運行時間是0.96秒。
line_profiler行代碼檢測
如果在只需要檢測函數的局部運行時間的話就可以使用line_profiler了,它可以檢測出每行代碼的運行時間。
line_profiler是python的非標準庫,使用的使用pip的方式安裝一下。
pip?install?line_profiler
最簡便的使用方式直接將需要測試的函數加入即可。
def?base_func3():
????for?n?in?range(10000):
????????print('當前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('當前n的值是:{}'.format(n))
從運行結果可以看出每行代碼的運行時間及比例,注意這里的時間單位是微妙。
heartrate可視化檢測
heartrate最值得推薦的是可以在網頁上面向檢測心率一樣檢測程序的執行過程,同時,他還是非標準庫,使用pip的方式進行安裝。
pip?install?heartrate
import?heartrate
heartrate.trace(browser=True)
def?base_func4():
????for?n?in?range(10000):
????????print('當前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
原文鏈接:https://mp.weixin.qq.com/s?__biz=MzA3ODk1Mzg0Mg==&mid=2649852129
相關推薦
- 2022-10-15 Python?pycharm提交代碼遇到沖突解決方法_python
- 2022-09-02 Python常用編碼的區別介紹_python
- 2023-11-18 python字符串string的截取;獲取字符串內的一串
- 2022-12-12 C++?Boost?Xpressive示例分析使用_C 語言
- 2022-05-31 利用python庫matplotlib繪制不同的圖表_python
- 2023-05-29 linux?rename?批量修改文件名的操作方法_linux shell
- 2024-03-17 bootloader配置權限過高會怎么樣?
- 2022-10-22 Kotlin淺析null操作方法_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支