一、前言
在我们在使用python进行编码的时候,写出高质量性能好的代码是一个比较好的习惯,line_profiler可很好的帮助我们
二、安装
1、下载
下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#line_profiler
下载想要安装的版本,比如,我这里的python版本是3.7x的,下载的whl就是对应的cp37的
2、使用pip安装
格式:pip install XXX.whl
3、执行结果
(CloudStorage) D:learnIOTCloudStorage>pip install C:Usersdurant.zengDesktop2.8.0line_profiler-3.1.0-cp37-cp37m-win_amd64.whl Processing c:usersdurant.zengdesktop2.8.0line_profiler-3.1.0-cp37-cp37m-win_amd64.whl Collecting IPython (from line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/c9/b1/82cbe2b856386f44f37fdae54d9b425813bd86fe33385c9d658d64826098/ipython-7.22.0-py3-none-any.whl (78 5kB) 100% |████████████████████████████████| 788kB 389kB/s Collecting prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 (from IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/eb/e6/4b4ca4fa94462d4560ba2f4e62e62108ab07be2e16a92e594e43b12d3300/prompt_toolkit-3.0.18-py3-none-any. whl (367kB) 100% |████████████████████████████████| 368kB 367kB/s Collecting decorator (from IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/ed/1b/72a1821152d07cf1d8b6fce298aeb06a7eb90f4d6d41acec9861e7cc6df0/decorator-4.4.2-py2.py3-none-any.wh l Collecting pygments (from IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/3a/80/a52c0a7c5939737c6dca75a831e89658ecb6f590fb7752ac777d221937b9/Pygments-2.8.1-py3-none-any.whl (98 3kB) 100% |████████████████████████████████| 993kB 420kB/s Requirement already satisfied: colorama; sys_platform == "win32" in d:python3.7.2libsite-packages (from IPython->line-profiler==3.1.0) (0.4.4) Requirement already satisfied: setuptools>=18.5 in d:python3.7.2libsite-packages (from IPython->line-profiler==3.1.0) (40.6.2) Collecting backcall (from IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/4c/1c/ff6546b6c12603d8dd1070aa3c3d273ad4c07f5771689a7b69a550e8c951/backcall-0.2.0-py2.py3-none-any.whl Collecting jedi>=0.16 (from IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/f9/36/7aa67ae2663025b49e8426ead0bad983fee1b73f472536e9790655da0277/jedi-0.18.0-py2.py3-none-any.whl (1 .4MB) 100% |████████████████████████████████| 1.4MB 445kB/s Collecting pickleshare (from IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/9a/41/220f49aaea88bc6fa6cba8d05ecf24676326156c23b991e80b3f2fc24c77/pickleshare-0.7.5-py2.py3-none-any. whl Collecting traitlets>=4.2 (from IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/f6/7d/3ecb0ebd0ce8dcdfa7bd47ab85c1d4a521e6770ef283d0824f5804994dfe/traitlets-5.0.5-py3-none-any.whl (1 00kB) 100% |████████████████████████████████| 102kB 373kB/s Collecting wcwidth (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/59/7c/e39aca596badaf1b78e8f547c807b04dae603a433d3e7a7e04d67f2ef3e5/wcwidth-0.2.5-py2.py3-none-any.whl Collecting parso<0.9.0,>=0.8.0 (from jedi>=0.16->IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/ad/f0/ef6bdb1eba2dbfda60c985cd8d7b47b6ed8c6a1f5d212f39ff50b64f172c/parso-0.8.1-py2.py3-none-any.whl (9 3kB) 100% |████████████████████████████████| 102kB 397kB/s Collecting ipython-genutils (from traitlets>=4.2->IPython->line-profiler==3.1.0) Downloading https://files.pythonhosted.org/packages/fa/bc/9bd3b5c2b4774d5f33b2d544f1460be9df7df2fe42f352135381c347c69a/ipython_genutils-0.2.0-py2.py3-none -any.whl Installing collected packages: wcwidth, prompt-toolkit, decorator, pygments, backcall, parso, jedi, pickleshare, ipython-genutils, traitlets, IPython, line- profiler Successfully installed IPython-7.22.0 backcall-0.2.0 decorator-4.4.2 ipython-genutils-0.2.0 jedi-0.18.0 line-profiler-3.1.0 parso-0.8.1 pickleshare-0.7.5 pr ompt-toolkit-3.0.18 pygments-2.8.1 traitlets-5.0.5 wcwidth-0.2.5
4、验证是否安装ok
import line_profiler,无报错
(CloudStorage) D:learnIOTCloudStorage>python Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> >>> >>> import line_profiler >>>
三、简单使用
测试案例
#!/usr/bin/python3 # -*- coding: UTF-8 -*- """ @author:durant.zeng @Description:描述 @file:yy.py @time:2021/03/30 """ import sys import time import line_profiler @line_profiler def test(): li = [] li_3 = [] li_x = [] for i in range(1000): if i % 2 == 0: li.append(i) elif i % 3 == 0: li_3.append(i) time.sleep(0.1) elif i % 5 == 0: x = i * [i for i in range(1000000)][1000] li_x.append(x) print(li_x) elif (i % 3 in [i for i in range(300000)]) or (i in [i for i in range(100000)]): print(i) print('--end--') profile = line_profiler.LineProfiler(test) # 把函数传递到性能分析器 profile.enable() # 开始分析 test() profile.disable() # 停止分析 profile.print_stats(sys.stdout) # 打印出性能分析结果
执行结果:
Total time: 44.4552 s File: D:/learn/IOT/CloudStorage/test/yy.py Function: test at line 14 Line # Hits Time Per Hit % Time Line Contents ============================================================== 14 def test(): 15 1 5.0 5.0 0.0 li = [] 16 1 2.0 2.0 0.0 li_3 = [] 17 1 2.0 2.0 0.0 li_x = [] 18 1001 13184.0 13.2 0.0 for i in range(1000): 19 1000 9276.0 9.3 0.0 if i % 2 == 0: 20 500 7372.0 14.7 0.0 li.append(i) 21 500 4638.0 9.3 0.0 elif i % 3 == 0: 22 167 566.0 3.4 0.0 li_3.append(i) 23 167 52138310.0 312205.4 37.6 time.sleep(0.1) 24 25 333 2463.0 7.4 0.0 elif i % 5 == 0: 26 67 35984802.0 537086.6 26.0 x = i * [i for i in range(1000000)][1000] 27 67 1090.0 16.3 0.0 li_x.append(x) 28 67 11033.0 164.7 0.0 print(li_x) 29 30 266 50385155.0 189417.9 36.4 elif (i % 3 in [i for i in range(300000)]) or (i in [i for i in range(100000)]): 31 266 37276.0 140.1 0.0 print(i) 32 266 6726.0 25.3 0.0 print('--end--')
- Total Time:测试代码的总运行时间
- Line:代码行号
- Hits:表示每行代码运行的次数
- Time:每行代码运行的总时间
- Per Hits:每行代码运行一次的时间
- % Time:每行代码运行时间的百分比
- 从分析结果可以看出:第23行代码运行时间最长
参考博客:https://blog.csdn.net/craftsman2020/article/details/109523731