美文网首页
Python性能优化-Profile

Python性能优化-Profile

作者: Franchen | 来源:发表于2019-02-17 00:27 被阅读0次

Why Profile

即使我们的代码已经非常Pythonic了,但运行效率还是不能满足要求。性能问题符合20/80规则,即20%的代码引起了80%的性能损耗。为了快速定位瓶颈代码,推荐通过Profile来分析,能到达事半功倍的效果。

Python Profile

对于Python,比较常用的Profile工具有三个:

  • profile 纯python语言实现,返回函数整体损耗。(自带)[详情]
  • cProfile 同profile,部分实现native化,返回函数整体损耗。(自带)
  • line_profile 返回函数每行损耗。(第三方)[详情]

Profile Usage

  • profile
# 这里使用vsm模型进行测试
from profile import Profile
p = Profile()
p.runcall(vsm, tags[0], tags[1])
p.print_stats()
 #  ncalls 函数总的调用次数
 #  tottime 函数内部(不包括子函数)的占用时间
 #  percall(第一个) tottime/ncalls
 #  cumtime 函数包括子函数所占用的时间
 #  percall(第二个)cumtime/ncalls
 #  filename:lineno(function)  文件:行号(函数)

   212 function calls in 0.002 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       78    0.000    0.000    0.000    0.000 :0(append)
       40    0.000    0.000    0.000    0.000 :0(count)
        2    0.000    0.000    0.000    0.000 :0(keys)
        3    0.000    0.000    0.000    0.000 :0(len)
       78    0.000    0.000    0.000    0.000 :0(pow)
        1    0.000    0.000    0.000    0.000 :0(range)
        1    0.001    0.001    0.001    0.001 :0(setprofile)
        1    0.000    0.000    0.000    0.000 :0(sqrt)
        1    0.000    0.000    0.002    0.002 profile:0(<function vsm at 0x000000000233A278>)
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.000    0.000 vsm.py:12(create_vocabulary)
        2    0.000    0.000    0.000    0.000 vsm.py:17(calc_tag_frequency)
        2    0.000    0.000    0.000    0.000 vsm.py:26(create_vector)
        1    0.000    0.000    0.001    0.001 vsm.py:38(calc_similar)
        1    0.000    0.000    0.001    0.001 vsm.py:52(vsm)
  • line_profile
# 这里使用vsm模型进行测试
from line_profiler import LineProfiler
lp = LineProfiler()
lp_vsm = lp(vsm)
lp_vsm(tags[0], tags[1])
lp.print_stats()
# Total Time:测试代码的总运行时间
# Line:代码行号 
# Hits:表示每行代码运行的次数 
# Time:每行代码运行的总时间 
# Per Hits:每行代码运行一次的时间 
# % Time:每行代码运行时间的百分比

Timer unit: 1e-07 s

Total time: 0.000219 s
File: vsm.py
Function: vsm at line 52

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    52                                           def vsm(tag_list1, tag_list2, debug=False):
    53         1         59.0     59.0      2.7      count = len(tag_list1) + len(tag_list2)
    54         1        123.0    123.0      5.6      vocabulary = create_vocabulary(tag_list1, tag_list2)
    55         1        677.0    677.0     30.9      vector1 = create_vector(calc_tag_frequency(tag_list1), vocabulary)
    56         1        530.0    530.0     24.2      vector2 = create_vector(calc_tag_frequency(tag_list2), vocabulary)
    57         1        777.0    777.0     35.5      similar = calc_similar(vector1, vector2, count)
    58         1         24.0     24.0      1.1      return similar

Profile GUI

虽然Profile的输出已经比较直观,但还是推荐保存profile结果,然后使用图形化工具从不同维度进行分析,更快地找出瓶颈代码。
常用的Profile图形化工具有:

QCacheGrind

相关文章

  • Python性能优化-Profile

    Why Profile 即使我们的代码已经非常Pythonic了,但运行效率还是不能满足要求。性能问题符合20/8...

  • Instruments

    iOS性能优化:Instruments工具 打开:Product - > Profile 1、Time Profi...

  • Python内存优化

    姓名:米芃 学号:16040520018 [嵌牛导读]Python内存优化的Profile工具,最有效的优化方法:...

  • iOS性能优化技巧

    通过静态 Analyze 工具,以及运行时 Profile 工具分析性能瓶颈,并进行性能优化。结合本人在开发中遇到...

  • Instruments介绍

    iOS性能优化工具: Instruments Time profile Energy Log 第一步 : 打开手...

  • Android-性能优化

    性能优化 卡顿 如何衡量卡顿 "卡顿" 产生的原因 Profile GPU Rendering 通用优化流程第一步...

  • 性能优化(timeline和profile)

    Profile## 简要:Profile里面提供了三种类型,通过这三种分析可以查看内存泄露和占用情况#### 收集...

  • 关于iOS APP的性能优化问题

    推荐博客文章: 1.iOS性能优化:Time Profile 2.iOS应用性能调优的25个建议和技巧 3.iOS...

  • golang profile用法

    概要 profile就是定时采样,收集cpu,内存等信息,进而给出性能优化指导,golang 官方提供了golan...

  • golang profile用法

    概要 profile就是定时采样,收集cpu,内存等信息,进而给出性能优化指导,golang 官方提供了golan...

网友评论

      本文标题:Python性能优化-Profile

      本文链接:https://www.haomeiwen.com/subject/txvveqtx.html