Python日学壹技:性能分析

简介: Python日学壹技:性能分析

导读

相信日常使用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.
相关文章
|
5月前
|
Python
170 python - 内置类型性能分析
170 python - 内置类型性能分析
25 0
|
数据可视化 中间件 API
Python性能分析利器pyinstrument讲解
Python性能分析利器pyinstrument讲解
471 0
Python性能分析利器pyinstrument讲解
|
11月前
|
算法 Python
python散列表实现查找,使用了多种算法并测试对比进行了性能分析(查找效率)
散列表实现查找 本章是填补之前文章的坑,对哈希算法进行了实现,使用了平方取中法/除留余数法进行哈希映射,使用开放地址与公共溢出区解决冲突,同时对不同方法进行了性能分析对比,最后进行了总结。 可以转载,但请声明源链接:文章源链接justin3go.com(有些latex公式某些平台不能渲染可查看这个网站)
96 0
|
Java Go Python
python 性能分析利器 py-spy
python 内存泄漏工具以及性能瓶颈分析工具分享。
4128 0
|
UED Python
如何进行 Python性能分析,你才能如鱼得水?
本文作者为 Bryan Helmig,主要介绍 Python 应用性能分析的三种进阶方案。文章系国内 ITOM 管理平台 OneAPM 编译呈现。
1545 0
|
14天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。

热门文章

最新文章