網(wǎng)站首頁 編程語言 正文
一、前言
程序的性能也是非常關(guān)鍵的指標(biāo),很多時候你的代碼跑的快,更能夠體現(xiàn)你的技術(shù)。最近發(fā)現(xiàn)很多小伙伴在性能分析的過程中都是手動打印運行時間的方式來統(tǒng)計代碼耗時的:
import datetime start=datetime.datetime.now() b=[i for i in range(10000000)] # 生成長度為一千萬的列表 end=datetime.datetime.now() print(end-start)
輸出結(jié)果
0:00:00.377766
這種方法使用很快捷,但需要統(tǒng)計每行代碼的執(zhí)行時間,生成可視化的報告等更完善的性能分析時就有點力不從心了。這個時候可以使用python的第三方庫Pyinstrument
來進(jìn)行性能分析。
二、Pyinstrument使用
Pyinstrument 是一個 Python 分析器。分析器是一種幫助您優(yōu)化代碼的工具 - 使其更快。要獲得最大的速度提升。
Pyinstrument 官方文檔:pyinstrument
Pyinstrument 安裝:
pip install pyinstrument
1. 舉例
對于最開始我們舉的例子,使用Pyinstrument實現(xiàn)的代碼如下:
文末添加個人VX,獲取資料和免費答疑 from pyinstrument import Profiler profiler=Profiler() profiler.start() b=[i for i in range(10000000)]# 生成長度為一千萬的列表 profiler.stop() profiler.print()
輸出結(jié)果
? _ ? ? ._ ? __/__ ? _ _ ?_ ?_ _/_ ? Recorded: 10:39:54 ?Samples: ?1
?/_//_/// /_\ / //_// / //_'/ // ? ? Duration: 0.385 ? ? CPU time: 0.391
/ ? _/ ? ? ? ? ? ? ? ? ? ? ?v4.1.1
Program: D:/code/server/aitestdemo/test2.py
0.385 <module> ?test2.py:2 ?#執(zhí)行總耗時
└─ 0.385 <listcomp> ?test2.py:7 #單行代碼耗時
打印的信息包含了記錄時間、線程數(shù)、總耗時、單行代碼耗時、CPU執(zhí)行時間等信息。
在多行代碼分析的情況下的使用效果(分別統(tǒng)計生成一千萬長度、一億長度、兩億長度的列表耗時):
from pyinstrument import Profiler profiler = Profiler() profiler.start() a = [i for i in range(10000000)] # 生成長度為一千萬的列表 b = [i for i in range(100000000)] # 生成長度為一億的列表 c = [i for i in range(200000000)] # 生成長度為十億的列表 profiler.stop() profiler.print()
輸出結(jié)果
Program: D:/code/server/aitestdemo/test2.py
16.686 <module> ?test2.py:1
├─ 12.178 <listcomp> ?test2.py:9
├─ 4.147 <listcomp> ?test2.py:8
└─ 0.358 <listcomp> ?test2.py:7
2. Pyinstrument分析django代碼
使用Pyinstrument分析 Django 代碼非常簡單,只需要在 Django 的配置文件settings.py
的 MIDDLEWARE
中添加如下配置:
MIDDLEWARE = [ ... 'pyinstrument.middleware.ProfilerMiddleware', ... ]
然后就可以在 url 上加一個參數(shù) profile 就可以:
Pyinstrument還支持flask、異步代碼的性能分析,具體可以查看官方文檔進(jìn)行學(xué)習(xí)。
Pyinstrument也提供了豐富的api供我們使用,官網(wǎng)文檔有詳細(xì)的介紹:https://pyinstrument.readthedocs.io/en/latest/reference.html
三、Pyinstrument與cProfile(python自帶性能分析器)的不同
根據(jù)官方文檔的描述來看,Pyinstrument的系統(tǒng)開銷會比cProfile 這類跟蹤分析器小很多,cProfile由于大量調(diào)用探查器,可能會扭曲測試結(jié)果:
總結(jié)
原文鏈接:https://blog.csdn.net/momoda118/article/details/122805206
相關(guān)推薦
- 2022-08-06 C#后臺調(diào)用WebApi接口的實現(xiàn)方法_C#教程
- 2023-10-27 獲取html中元素的寬高
- 2022-01-19 正則——時間 時分秒 12小時制 24小時制 moment可以轉(zhuǎn)化的時間 HH:mm:ss hh:m
- 2022-07-06 c#?復(fù)寫Equals方法的實現(xiàn)_C#教程
- 2022-08-03 Django框架中模型的用法_python
- 2022-05-31 C語言可變參數(shù)與函數(shù)參數(shù)的內(nèi)存對齊詳解_C 語言
- 2023-01-30 React合成事件及Test?Utilities在Facebook內(nèi)部進(jìn)行測試_React
- 2022-08-23 iOS底層探索之自動釋放池原理解析_IOS
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支