網站首頁 編程語言 正文
一、前言
程序的性能也是非常關鍵的指標,很多時候你的代碼跑的快,更能夠體現你的技術。最近發現很多小伙伴在性能分析的過程中都是手動打印運行時間的方式來統計代碼耗時的:
import datetime start=datetime.datetime.now() b=[i for i in range(10000000)] # 生成長度為一千萬的列表 end=datetime.datetime.now() print(end-start)
輸出結果
0:00:00.377766
這種方法使用很快捷,但需要統計每行代碼的執行時間,生成可視化的報告等更完善的性能分析時就有點力不從心了。這個時候可以使用python的第三方庫Pyinstrument
來進行性能分析。
二、Pyinstrument使用
Pyinstrument 是一個 Python 分析器。分析器是一種幫助您優化代碼的工具 - 使其更快。要獲得最大的速度提升。
Pyinstrument 官方文檔:pyinstrument
Pyinstrument 安裝:
pip install pyinstrument
1. 舉例
對于最開始我們舉的例子,使用Pyinstrument實現的代碼如下:
文末添加個人VX,獲取資料和免費答疑 from pyinstrument import Profiler profiler=Profiler() profiler.start() b=[i for i in range(10000000)]# 生成長度為一千萬的列表 profiler.stop() profiler.print()
輸出結果
? _ ? ? ._ ? __/__ ? _ _ ?_ ?_ _/_ ? 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 ?#執行總耗時
└─ 0.385 <listcomp> ?test2.py:7 #單行代碼耗時
打印的信息包含了記錄時間、線程數、總耗時、單行代碼耗時、CPU執行時間等信息。
在多行代碼分析的情況下的使用效果(分別統計生成一千萬長度、一億長度、兩億長度的列表耗時):
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()
輸出結果
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 上加一個參數 profile 就可以:
Pyinstrument還支持flask、異步代碼的性能分析,具體可以查看官方文檔進行學習。
Pyinstrument也提供了豐富的api供我們使用,官網文檔有詳細的介紹:https://pyinstrument.readthedocs.io/en/latest/reference.html
三、Pyinstrument與cProfile(python自帶性能分析器)的不同
根據官方文檔的描述來看,Pyinstrument的系統開銷會比cProfile 這類跟蹤分析器小很多,cProfile由于大量調用探查器,可能會扭曲測試結果:
總結
原文鏈接:https://blog.csdn.net/momoda118/article/details/122805206
相關推薦
- 2022-07-12 計算SHA-1摘要值,并轉為16進制字符串
- 2022-11-25 Python?Django教程之模型中字段驗證詳解_python
- 2022-02-19 springboot log4j2-dev.xml打成war包部署到tomcat無效
- 2022-08-15 前端如何解決瀏覽器自動填充input輸入框賬號密碼的問題
- 2022-06-09 Python+OpenCV實現圖片中的圓形檢測_python
- 2022-04-28 oracle重置序列從0開始遞增1_oracle
- 2022-04-14 Django執行指定腳本的幾種方法_python
- 2022-05-12 C語言的數組指針與函數指針詳解_C 語言
- 最近更新
-
- 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同步修改后的遠程分支