Python的N种性能测试工具(timeit、profile、cProfile、line_profiler、memory_profiler、objgraph、Pyinstrument、PyCharm)
  TEZNKK3IfmPf 2023年11月13日 14 0

1、timeit

        timeit只输出被测试代码的总运行时间,单位为秒,没有详细的统计。

示例代码:

import timeit


def fun():
    lst = []
    for i in range(100000):
        lst.append(i * i)


print(timeit.timeit('fun()', 'from __main__ import fun', number=1))
print(timeit.timeit('fun()', 'from __main__ import fun', number=100))

运行结果:

Python的N种性能测试工具(timeit、profile、cProfile、line_profiler、memory_profiler、objgraph、Pyinstrument、PyCharm)

2、profile

profile:纯Python实现的性能测试模块,接口和cProfile一样。

示例代码:

import profile


def fun():
    lst = []
    for i in range(100000):
        lst.append(i * i)


print(profile.run('fun()'))

运行结果:

Python的N种性能测试工具(timeit、profile、cProfile、line_profiler、memory_profiler、objgraph、Pyinstrument、PyCharm)

参数解析:

  • ncall:函数运行次数
  • tottime: 函数的总的运行时间,减去函数中调用子函数的运行时间
  • 第一个percall:percall = tottime / nclall
  • cumtime:函数及其所有子函数调整的运行时间,也就是函数开始调用到结束的时间。
  • 第二个percall:percall = cumtime / nclall 

3、cProfile

        c语言实现的性能测试模块,接口和profile一样。

示例代码:

import cProfile


def fun():
    lst = []
    for i in range(100000):
        lst.append(i * i)


print(cProfile.run('fun()'))

运行结果:

Python的N种性能测试工具(timeit、profile、cProfile、line_profiler、memory_profiler、objgraph、Pyinstrument、PyCharm)

参数解析:

        ncalls、tottime、percall、cumtime含义同profile。

4、line_profiler

        略

5、memory_profiler

        略

6、objgraph

        略

7、Pyinstrument

详见博文:python中代码性能分析Pyinstrument库_IT之一小佬的博客-CSDN博客

8、PyCharm图形化性能测试工具

详见博文:PyCharm的Profile工具进行python代码性能分析_IT之一小佬的博客-CSDN博客

参考博文:

Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph_xiemanR的博客-CSDN博客_python cprofile flask 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   32   0   0 python开发语言
  TEZNKK3IfmPf   2024年05月31日   25   0   0 python
  TEZNKK3IfmPf   2024年05月31日   34   0   0 excelpython
  TEZNKK3IfmPf   2024年05月31日   25   0   0 python
TEZNKK3IfmPf