首页 > 编程语言 >Python日学壹技:性能分析

Python日学壹技:性能分析

时间:2022-11-30 12:14:14浏览次数:59  
标签:profile 50000 MiB Python 壹技 日学 profiler memory line

导读

相信日常使用Python作为生产力的读者,一定会存在想要分析代码中每一行的运行时间与变量占用内存大小的需求,本文主要分析两个模块,用于分析每行代码的内存使用情况和运行时间情况。

内存使用

安装

pip install memory-profiler

使用方法一

  1. 在需要分析的函数上,添加装饰器@profile
@profile
def test1():
	c=0
	for item in xrange(100000):
 		c+=1
 	print (c)
        
  1. 使用下面的命令运行
python -m memory_profiler memory_profiler_test.py     

使用方法二

 from memory_profiler import profile
 
 @profile(precision=4,stream=open('memory_profiler.log','w+'))
# @profile
def test1():
     c=0
     for item in xrange(100000):
         c+=1
     print c

# 直接运行即可    

结果

Filename: memory_profiler_test.py

Line #    Mem usage    Increment   Line Contents
================================================
     5   21.492 MiB   21.492 MiB   @profile
     6                             def test1():
     7   21.492 MiB    0.000 MiB       c=0
     8   21.492 MiB    0.000 MiB       for item in xrange(100000):
     9   21.492 MiB    0.000 MiB           c+=1
    10   21.492 MiB    0.000 MiB       print c
  • Mem usage: 内存占用情况
  • Increment: 执行该行代码后新增的内存

运行时间

安装

pip install line-profiler

使用

  1. 在需要分析的函数上,添加装饰器@profile
@profile
def slow_function(a, b, c):
    ...
  1. 运行
python -m line_profiler script_to_profile.py.lprof

结果

Pystone(1.1) time for 50000 passes = 2.48
This machine benchmarks at 20161.3 pystones/second
Wrote profile results to pystone.py.lprof
Timer unit: 1e-06 s

File: pystone.py
Function: Proc2 at line 149
Total time: 0.606656 s

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   149                                           @profile
   150                                           def Proc2(IntParIO):
   151     50000        82003      1.6     13.5      IntLoc = IntParIO + 10
   152     50000        63162      1.3     10.4      while 1:
   153     50000        69065      1.4     11.4          if Char1Glob == 'A':
   154     50000        66354      1.3     10.9              IntLoc = IntLoc - 1
   155     50000        67263      1.3     11.1              IntParIO = IntLoc - IntGlob
   156     50000        65494      1.3     10.8              EnumLoc = Ident1
   157     50000        68001      1.4     11.2          if EnumLoc == Ident1:
   158     50000        63739      1.3     10.5              break
   159     50000        61575      1.2     10.1      return IntParIO
  • 每列含义
> - Line #: The line number in the file.
> - Hits: The number of times that line was executed.
> - Time: The total amount of time spent executing the line in the timer's units. In the header information before the tables, you will see a line "Timer unit:" giving the conversion factor to seconds. It may be different on different systems.
> - Per Hit: The average amount of time spent executing the line once in the timer's units.
> - % Time: The percentage of time spent on that line relative to the total amount of recorded time spent in the function.
> - Line Contents: The actual source code. Note that this is always read from disk when the formatted results are viewed, *not* when the code was executed. If you have edited the file in the meantime, the lines will not match up, and the formatter may not even be able to locate the function for display.

欢迎Star -> 学习目录

更多教程 -> 学习目录


本文由mdnice多平台发布

标签:profile,50000,MiB,Python,壹技,日学,profiler,memory,line
From: https://www.cnblogs.com/swindler/p/16938020.html

相关文章

  • 1python解释器的下载和安装
    1.1python解释器:一款用于执行python代码的应用程序。下载网址:https://www.python.org/downloads选择自定义安装,添加环境变量1.2pycharm下载和配置安装完成后进行许可证激......
  • python multiprocessing使用容易遇到的坑记录随笔
    python因为有GIL(GlobalInterpreterLock)锁的问题,所以在计算密集型程序中,推荐使用multiprocessing多进程编程。在使用multiprocessing创建子进程时,很容易遇到一个不易发现......
  • day02 Python基础
    1.Python语法1.1编码使用工具写汉字、字母、数字,写完之后,你是需要保存到硬盘上卢慧yyds66601010101010101101010101001010101000111101一套编......
  • day01 环境搭建及python介绍
    1.Typora安装为什么要使用Typora的软件呢?是因为程序员不只是写代码这一件事,还需要给编写的代码写README文档,这个文档是说明程序如何使用的,README编写使用的就是Markd......
  • Python 中 -m 的典型用法、原理解析与发展演变
    在命令行中使用Python时,它可以接收大约20个选项(option),语法格式如下:python[-bBdEhiIOqsSuvVWx?][-ccommand|-mmodule-name|script|-][args]本文想要聊聊比较......
  • Python 依赖库管理哪家强?pipreqs、pigar、pip-tools、pipdeptree 任君挑选
    在Python的项目中,如何管理所用的全部依赖库呢?最主流的做法是维护一份“requirements.txt”,记录下依赖库的名字及其版本号。那么,如何来生成这份文件呢?在上篇文章《​​由浅......
  • 开发者请注意:Python2 的最后版本将于 4 月发布,但它确实是在 1 月 1 日就寿命终止了!
    2020年1月1日是Python2的寿命终止日,这个日期在两年前经"Python之父"GuidovanRossum宣布,此后一直成为开发者社区翘首以盼的一天。昨天就是这个大快人心的日子,各种......
  • How to Install Python on Linux
    SummaryHostmonsterusesthepreinstalledversionofPythonthatshipswithCentOS.Becauseofthisitisoftennotthelatestrelease.Thisarticlewillexplain......
  • Python工具箱系列(十七)
    很多软件工程师都认为MD5是一种加密算法,然而这种观点是不对的。作为一个1992年第一次被公开的算法,到今天为止已经被发现了一些致命的漏洞。本文讨论MD5在密码保存方面的......
  • python编辑excel表格文件的简单方法练习
    一、创建一个Excel文件fromopenpyxlimportWorkbook#需要用到openpyxl模块来操作Excel文件。openpyxl需要先安装。#实例化对象wb=Workbook()#获取当前active的shee......