这篇博客简单介绍一些python性能分析的常用工具, 性能分析主要是代码运行的时间和内存分析,希望能给大家提供帮助

通过time模块

import time
def test(num_iterations):a = 1for i in range(num_iterations):a *= -1return anum_iterations = 1_000_000_0
t1 = time.time()
res = test(num_iterations)
t2 = time.time()
print(t2 - t1)

这可能是最常用的方法, 我们可以用这个方法测试某一行代码或者某一个函数的运行时间

定义一个装饰器

def time_wrapper(func):def measure_time(*args, **kwargs):t1 = time.time()result = func(*args, **kwargs)t2 = time.time()print(f"{func.__name__} took {t2 - t1} seconds")return resultreturn measure_time
@time_wrapper
def test(num_iterations):a = 1for i in range(num_iterations):a *= -1return ares = test(num_iterations)
# test took 0.4167180061340332 seconds

通过装饰器time_wrapper, 我们可以在任意的函数上加上@time_wrapper,非常方便

用timeit模块测量执行速度

在juputer中,我们甚至还可以使用%timeit来测量速度

# 循环次数(-n 2)和重复次数(-r 2)
# timeit会对语句循环执行n次并计算平均值作为一个结果,重复r次选择最好的那个
%timeit -n 2 -r 2 test(num_iterations)
"""
test took 0.4153749942779541 seconds
test took 0.4038848876953125 seconds
test took 0.40583109855651855 seconds
test took 0.40905189514160156 seconds
409 ms ± 1.09 ms per loop (mean ± std. dev. of 2 runs, 2 loops each)
"""

当然也可以在terminal中进行测试

$python -m timeit -n 2 -r 2 -s "import numpy as np" "np.array(10)"# 2 loops, best of 2: 2.52 usec per loop

使用unix time命令进行简单的计时

%usr/bin/time -p python test.py
  • real记录了整体的耗时
  • user记录了CPU花在任务上的时间,不包括内核函数耗费的时间
  • sys记录了内核函数耗费的时间

使用cProfile模块

cProfile是一个标准的内建分析工具,它钩入CPython虚拟机来测量其每一个函数运行所花费的时间,这会引入巨大开销,但会获得更多的信息

def test1(value, num_iterations):res = 1for i in range(num_iterations):res += res * 2 % 10return valuedef test2(num_iterations):res = 0for i in range(num_iterations):res += i*i % 10test1(1, num_iterations)return resif __name__ == '__main__':#-s cumulative对每个函数累计花费时间进行排序"""python -m cProfile -s cumulative test_cprofiler.py"""# 生成一个统计文件"""python -m cProfile -o profile.stats test_cprofiler.py"""print('hello world')num_iterations = 1_000_000_0res1 = test1(1, num_iterations)res2 = test2(num_iterations)"""
<pstats.Stats object at 0x1115f29d0>
Sun May 10 19:30:19 2020    profile.stats5 function calls in 2.221 secondsOrdered by: cumulative timencalls  tottime  percall  cumtime  percall filename:lineno(function)1    0.000    0.000    2.221    2.221 {built-in method builtins.exec}1    0.000    0.000    2.221    2.221 test_cprofiler.py:1(<module>)1    1.303    1.303    1.303    1.303 test_cprofiler.py:8(test2)1    0.918    0.918    0.918    0.918 test_cprofiler.py:1(test1)1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}<pstats.Stats at 0x1115f29d0>
"""
  • for the total time spent in the given function (and excluding time made in calls to sub-functions)
  • cumulative time spent in this and all subfunctions (from invocation till exit). This figure is accurate even for recursive functions.

用line_profiler进行逐行分析

line_profiler是调查CPU密集型问题的最强大工具, 它可以对函数进行逐行的分析。可以先用cProfiler找到需要分析的函数,然后用line_profiler对函数进行分析

def test1(value, num_iterations):res = 1for i in range(num_iterations):res += res * 2 % 10return value@profile
def test2(num_iterations):res = 0for i in range(num_iterations):res += i*i % 10return resif __name__ == '__main__':# 用装饰器@profile标记选中的函数,用kernprof.py来运行代码# -l代表逐行分析,-v用于显示输出"""kernprof -l -v test_lineprofiler.py"""num_iterations = 1_000_000_0res1 = test1(1, num_iterations)res2 = test2(num_iterations)"""
Wrote profile results to test_lineprofiler.py.lprof
Timer unit: 1e-06 sTotal time: 6.1838 s
File: test_lineprofiler.py
Function: test2 at line 7Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================7                                           @profile8                                           def test2(num_iterations):9         1        231.0    231.0      0.0    res = 010  10000001    2488600.0      0.2     40.2    for i in range(num_iterations):11  10000000    3694972.0      0.4     59.8      res += i*i % 1012         1          1.0      1.0      0.0    return res
"""
  • Timer unit: 1e-06 s:时间单位;
  • Total time: 0.004891 s:总时间;
  • Hit:代码运行次数;
  • %Time:代码占了它所在函数的消耗的时间百分比,通常直接看这一列。

用memory_profiler诊断内存用量

memory_profiler的操作和line_profiler十分类似, 但是运行速度要慢的多. 内存分析可以轻易让你的代码慢上10倍到100倍。所以实际操作可能只是偶尔使用memory_profiler而更多的使用line_profiler来进行CPU分析

import numpy as np
from memory_profiler import profile@profile
def test2(num_iterations):res = 0for i in range(num_iterations):res += i*i % 10b = np.random.rand(1000, 1000)a = [0] * 1000000return res, b, aif __name__ == '__main__':# 在函数前添加 @profile"""python -m memory_profiler test_memoryprofiler.py"""num_iterations = 1_000res2 = test2(num_iterations)"""
Line #    Mem usage    Increment   Line Contents
================================================5     55.6 MiB     55.6 MiB   @profile6                             def test2(num_iterations):7     55.6 MiB      0.0 MiB     res = 08     55.6 MiB      0.0 MiB     for i in range(num_iterations):9     55.6 MiB      0.0 MiB       res += i*i % 1010     63.2 MiB      7.7 MiB     b = np.random.rand(1000, 1000)11     70.9 MiB      7.6 MiB     a = [0] * 100000012     70.9 MiB      0.0 MiB     return res, b, a
"""
  • Mem usage : 运行到当前位置使用内存
  • Increment : 运行当前代码后,增加的内存

python程序性能分析相关推荐

  1. C语言火焰图,Python程序性能分析和火焰图

    之前做过一个Python程序,用来解析Excel文件,经过一串复杂的处理,导出成其他不同格式的文件.随着需要处理的Excel文件越来越多,程序的执行时间也越来越长,需要对性能进行优化. 性能优化首先要 ...

  2. Go程序性能分析pprof

    from: Go程序性能分析pprof     参考: http://blog.golang.org/profiling-go-programs http://google-perftools.goo ...

  3. Golang程序性能分析(三)用pprof分析gRPC服务的性能

    这是Golang程序性能分析系列文章的最后一篇,这次我们的主要内容是如何使用pprof工具对gRPC服务的程序性能进行分析.关于gRPC这个框架的文章之前已经写过不少文章了,如果你对它还不太熟悉,不知 ...

  4. Golang程序性能分析(二)在Echo和Gin框架中使用pprof

    前言 今天继续分享使用Go官方库pprof做性能分析相关的内容,上一篇文章:Golang程序性能分析(一)pprof和go-torch中我花了很大的篇幅介绍了如何使用pprof采集Go应用程序的性能指 ...

  5. 程序性能分析及性能测试

    这里所说的程序是指对外提供tcp/ip交互协议的服务性程序.网络程序性能分析很重要,比如随着网络请求流量越来越大,我们需要知道已部署的服务能不能满足需求.这里采用对网络服务程序进行建模的方法分析影响程 ...

  6. linux java火焰图_Linux程序性能分析和火焰图

    Linux程序性能分析和火焰图 Linux程序的性能分析工具数量比较多,涉及到整个操作系统的方方面面,可能是开源的原因吧,相对于Windows来说丰富太多.其中应用分析性能方面Dtrace, Syst ...

  7. golang程序性能分析

    最近在使用GraphQL编写golang程序,但GraphQL框架在golang上的实践比较少,很多性能上的资料也不够全面.考虑到线上抗压的问题,笔者决定对自己开发的服务模块进行性能压测,评估下服务的 ...

  8. python append函数_Python程序性能分析

    本文主要参考<High Performance Python>[1]Chapter 2: Profiling to Find Bottlenecks. 被测试程序为julia.py,由于代 ...

  9. 【程序性能分析利器】Google Perf Tool 和 Valgrind 工具包简介

    Google Perf Tools 的安装和使用 Gperf 工具包包含如下几个工具: 一个优化的内存管理算法-tcmalloc性能优于malloc. 一个用于CPU profile的工具,用于检测程 ...

最新文章

  1. 机器学习 | 数据从哪里找?手把手教你构建数据集
  2. 将pdf转换html_pdf文件怎么转换成html网页格式?用什么方法来转换?
  3. 【线性规划与网络流24题】汽车加油行驶问题 分层图
  4. 我的sublime配置
  5. Cannot open include file: jni.h: No such file or directory解决方法
  6. 浏览器字体大小设置_CSS之 浏览器解析样式的过程
  7. 性能监控工具javamelody与spring的集成
  8. UI标签库专题九:JEECG智能开发平台 Choose(选则操作标签)
  9. win7旗舰版+caffe+vs2013+matlab2014b(无GPU版)
  10. idea 添加 golang 项目的 gopath
  11. LinkedList遍历方式区别
  12. Python学习笔记——python基础 4. 函数进阶
  13. JAVA基础之设计模式和枚举
  14. xls与csv文件区别
  15. Jupyter内的文件保存
  16. 家里宽带网络连接第二台路由器实验一
  17. NLU(Natural Language Understanding)太难了
  18. sublime build 系统必读
  19. 鸿蒙测试麒麟920,麒麟920打MT6595没压力
  20. python处理PPOCRLabel标注的数据用于LPRNet与Yolo的训练

热门文章

  1. 02.docker镜像的区别--Alpine、Slim、Stretch、Buster、Jessie、Bullseye
  2. 面向对象设计的六大原则
  3. 不是python内置函数的是_Python内置函数
  4. 你应该知道的计算机网络知识 【转】
  5. linux车机端carlife移植、开发
  6. QTextEdit和QTextDocument(ZZ)
  7. Doom Emacs入门:通过Doom Emacs框架搭建一个基本的Python开发环境及其基本操作
  8. pandas数据分析模块(二)
  9. 嫦娥四号工程总共投入是多少?国家航天局回应
  10. 【论文阅读】MIMICS: A Large-Scale Data Collection for Search Clarification