python函数运行内存时间等性能检测工具

作者:Sir?老王 时间:2021-04-08 02:24:00 

python虽然是一门'慢语言',但是也有着比较多的性能检测工具来帮助我们优化程序的运行效率。

这里总结了五个比较好的python性能检测工具,包括内存使用、运行时间、执行次数等方面。

基础测试函数

首先,来编写一个基础的python函数用于在后面的各种性能测试。

def base_func():
    for n in range(10000):
        print('当前n的值是:{}'.format(n))

memory_profiler进程

memory_profiler是python的非标准库,所以这里采用pip的方式进行安装。它能够监视进程、了解内存使用等情况。

pip install memory_profiler

安装好memory_profiler库以后,直接使用注解的方式进行测试。

from memory_profiler import profile
@profile
def base_func1():
    for n in range(10000):
        print('当前n的值是:{}'.format(n))
base_func1()
# Line #    Mem usage    Increment  Occurrences   Line Contents
# =============================================================
#     28     45.3 MiB     45.3 MiB           1   @profile
#     29                                         def base_func():
#     30     45.3 MiB      0.0 MiB       10001       for n in range(10000):
#     31     45.3 MiB      0.0 MiB       10000           print('当前n的值是:{}'.format(n))

从返回的数据结果来看,执行当前函数使用了45.3 MiB的内存。

timeit 时间使用情况

timeit是python的内置模块,可以测试单元格的代码运行时间,由于是内置模块所以并不需要单独安装。

import timeit
def base_func2():
    for n in range(10000):
        print('当前n的值是:{}'.format(n))
res = timeit.timeit(base_func2,number=5)
print('当前的函数的运行时间是:{}'.format(res))
# 当前的函数的运行时间是:0.9675800999999993

根据上面函数的运行返回结果,函数的运行时间是0.96秒。

line_profiler行代码检测

如果在只需要检测函数的局部运行时间的话就可以使用line_profiler了,它可以检测出每行代码的运行时间。

line_profiler是python的非标准库,使用的使用pip的方式安装一下。

pip install line_profiler

最简便的使用方式直接将需要测试的函数加入即可。

def base_func3():
    for n in range(10000):
        print('当前n的值是:{}'.format(n))
from line_profiler import LineProfiler
lp = LineProfiler()
lp_wrap = lp(base_func3)
lp_wrap()
lp.print_stats()
# Line #      Hits         Time  Per Hit   % Time  Line Contents
# ==============================================================
#     72                                           def base_func3():
#     73     10001     162738.0     16.3      4.8      for n in range(10000):
#     74     10000    3207772.0    320.8     95.2          print('当前n的值是:{}'.format(n))

从运行结果可以看出每行代码的运行时间及比例,注意这里的时间单位是微妙。

heartrate可视化检测

heartrate最值得推荐的是可以在网页上面向检测心率一样检测程序的执行过程,同时,他还是非标准库,使用pip的方式进行安装。

pip install heartrate
import heartrate
heartrate.trace(browser=True)
def base_func4():
    for n in range(10000):
        print('当前n的值是:{}'.format(n))

运行以后,控制台打印如下日志:

#  * Serving Flask app "heartrate.core" (lazy loading)
#  * Environment: production
#    WARNING: This is a development server. Do not use it in a production deployment.
#    Use a production WSGI server instead.
#  * Debug mode: off

并且自动打开浏览器地址:http://127.0.0.1:9999

python函数运行内存时间等性能检测工具

来源:https://mp.weixin.qq.com/s?__biz=MzA3ODk1Mzg0Mg==&mid=2649852129

标签:python,性能检测,函数运行
0
投稿

猜你喜欢

  • 在ASP中用“正则表达式对象”来校验数据的合法性

    2010-05-27 12:25:00
  • Python tkinter实现计算器功能

    2023-06-29 15:41:29
  • 四种Python机器学习超参数搜索方法总结

    2022-03-19 17:29:22
  • 动态给表添加删除字段并同时修改它的插入更新存储过程

    2011-12-01 10:18:28
  • python爬取王者荣耀全皮肤的简单实现代码

    2021-03-14 16:36:43
  • sql ntext数据类型字符替换实现代码

    2011-09-30 11:08:00
  • 带你了解Python妙开根号的三种方式

    2021-10-18 08:27:56
  • PHP实现HTML页面静态化的方法

    2023-10-15 04:09:59
  • 在Python中使用__slots__方法的详细教程

    2022-10-30 09:46:10
  • Excel和Access之间的数据交换

    2008-11-20 16:53:00
  • WEB2.0网页制作标准教程(1)选择什么样的DOCTYPE

    2007-11-13 12:57:00
  • PHP中curl_setopt函数用法实例分析

    2023-11-22 22:07:22
  • asp我对后台安全的一些做法

    2011-09-01 19:22:09
  • PHP 数组和字符串互相转换实现方法

    2023-06-19 15:04:17
  • python 如何快速复制序列

    2022-12-04 05:20:36
  • 简单介绍Python中用于求最小值的min()方法

    2021-05-27 23:00:11
  • 对比国内门户网站对Flash激活限制的处理

    2007-08-23 11:29:00
  • 得到字符串真实长度和取固定长度的字符串函数

    2008-10-06 13:12:00
  • asp如何自动更新导航栏?

    2010-07-07 12:10:00
  • 从绘画语言的发展,看视觉设计风格

    2008-08-03 17:11:00
  • asp之家 网络编程 m.aspxhome.com