掌握Python系统管理-调试和分析脚本2- cProfile和timeit
调试和分析在Python开发中发挥着重要作用。 调试器可帮助程序员分析完整的代码。 调试器设置断点,而剖析器运行我们的代码,并给我们执行时间的详细信息。 分析器将识别程序中的瓶颈。我们将了解pdb Python调试器,cProfile模块和timeit模块来计算Python代码的执行时间。
涉及内容:
- Python调试技术
- 错误处理(异常处理)
- 调试工具
- 调试基本程序崩溃
- 分析和计时程序
- 使程序运行得更快
跟踪程序
trace_example.py
class Student:
def __init__(self, std):
self.count = std def go(self):
for i in range(self.count):
print(i)
return
if __name__ == '__main__':
Student(5).go()
执行:
$ python3 -m trace --trace trace_example.py
--- modulename: trace_example, funcname: <module>
trace_example.py(1): class Student:
--- modulename: trace_example, funcname: Student
trace_example.py(1): class Student:
trace_example.py(2): def __init__(self, std):
trace_example.py(5): def go(self):
trace_example.py(9): if __name__ == '__main__':
trace_example.py(10): Student(5).go()
--- modulename: trace_example, funcname: __init__
trace_example.py(3): self.count = std
--- modulename: trace_example, funcname: go
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
0
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
1
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
2
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
3
trace_example.py(6): for i in range(self.count):
trace_example.py(7): print(i)
4
trace_example.py(6): for i in range(self.count):
trace_example.py(8): return
--- modulename: trace, funcname: _unsettrace
trace.py(77): sys.settrace(None)
参考资料
Q群内免费获取887934385
分析和计时程序
分析Python程序意味着测量程序的执行时间。它衡量每个功能所花费的时间。 Python的cProfile模块用于分析Python程序。
cProfile模块
如前所述,分析意味着测量程序的执行时间。我们将使用cProfile Python模块来分析程序。
现在,我们将编写一个cprof_example.py脚本并在其中编写以下代码:
mul_value = 0 def mul_numbers( num1, num2 ):
mul_value = num1 * num2;
print ("Local Value: ", mul_value)
return mul_value mul_numbers( 58, 77 )
print ("Global Value: ", mul_value)
执行:
$ python3 -m cProfile cprof_example.py
Local Value: 4466
Global Value: 0
6 function calls in 0.000 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 cprof_example.py:1(<module>)
1 0.000 0.000 0.000 0.000 cprof_example.py:3(mul_numbers)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
2 0.000 0.000 0.000 0.000 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
- ncalls:调用次数
- tottime:函数花费的总时间
- percall:tottime/ncalls
- cumtime:函数及其子函数中花费的累计时间
- percall:cumtime/primitive calls
filename:lineno(function):函数信息
使用timeit,我们可以决定我们想要测量哪些代码的性能。因此,我们可以轻松定义设置代码以及我们要单独执行测试的代码段。主代码运行100万次,这是默认时间,而设置代码只运行一次。
import timeit prg_setup = "from math import sqrt"
prg_code = '''
def timeit_example():
list1 = []
for x in range(50):
list1.append(sqrt(x))
'''
# timeit statement
print(timeit.timeit(setup = prg_setup, stmt = prg_code, number = 10000))
执行:
$ python timeit_example.py
0.00180888175964
使程序运行得更快
有多种方法可以使Python程序运行得更快,如下所示:
- Profile代码,以便识别瓶颈
- 使用内置函数和库,因此解释器不需要执行循环
- 避免使用全局变量,因为Python在访问全局变量时非常慢
- 使用已有包
小结
在本章中,我们了解了调试和分析程序的重要性。我们了解了可用于调试的不同技术。
我们了解了pdb Python调试器以及如何处理异常。我们在分析和计时脚本时学习了如何使用Python的cProfile和timeit模块。我们还学习了如何使脚本运行得更快。
掌握Python系统管理-调试和分析脚本2- cProfile和timeit的更多相关文章
- 自制基于python的DoU log分析脚本
工作中测试DoU的log需要分析,原先是使用excel,去ctrl c,ctrl v截取数据,整理格式等等.一来,这工作虽然很简单,但是非常耗时,不熟练的人(比如我)一搞搞个半天:二来,不小心还会出现 ...
- python编写网络抓包分析脚本
python编写网络抓包分析脚本 写网络抓包分析脚本,一个称手的sniffer工具是必不可少的,我习惯用Ethereal,简单,易用,基于winpcap的一个开源的软件 Ethereal自带许多协议的 ...
- eos源码分析和应用(一)调试环境搭建
转载自 http://www.limerence2017.com/2018/09/02/eos1/#more eos基于区块链技术实现的开源引擎,开发人员可以基于该引擎开发DAPP(分布式应用).下面 ...
- 【转载】Python 代码调试技巧
https://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/ Python 代码调试技巧 张 颖2012 年 5 月 03 日发布 ...
- Python Telnet弱口令爆破脚本及遇到的错误与问题
写得时候遇到了一个很大的问题,就是我在发送用户名,接受用户名就会一直卡住.然后等了好久后提示 recv ‘\r\nSession timed out.\r\n\r\nTelnet Server has ...
- Python、Lua和Ruby——脚本大P.K.
转自Python.Lua和Ruby--脚本大P.K. Python versus Lua Versus Ruby Python.Lua和Ruby--脚本大P.K. Tom Gutschmidt 著 赖 ...
- BASE64编码原理分析脚本实现及逆向案例
在互联网中的每一刻,你可能都在享受着Base64带来的便捷,但对于Base64的基础原理你又了解多少?今天小编带大家了解一下Base64编码原理分析脚本实现及逆向案例的相关内容. 01编码由来 数 ...
- day31 堡垒机尾声 + Python与金融量化分析(一)
堡垒机尾声: 代码案例:https://github.com/liyongsan/git_class/tree/master/day31 课堂笔记:file send: 1.选择本地文件 2.远程路径 ...
- 调试 ASP 程序脚本
调试 ASP 脚本 无论您的计划多么精密.经验多么丰富,脚本错误 (bug) 可能在最初就使您的 ASP 服务器端的脚本无法正确运行.也就是说调试,即查找和纠正脚本错误,对开发一个成功的和强健的 AS ...
随机推荐
- (三)django--带Template的网站
我们接着(二),在new下新建一个templates(注意必须是这个名字,不然要进行配置),然后在该目录下新建一个test.html 2.我们在pycharm中新建的html文件里面不会有内容,我们输 ...
- LeetCode 二叉树的层次遍历
第102题 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...
- python基础-数字类型及内置方法
--数字类型及内置方法 整型-int 用途:多用于年龄.电话.QQ号等变量 定义方法 age = 18 # age = int(18) 常用方式:多用于数学计算 # int(x)将x转换成整数,是向下 ...
- Jenkins下载地址
http://ftp.yz.yamagata-u.ac.jp/pub/misc/jenkins/war/ Jenkins插件下载: http://updates.jenkins-ci.org/down ...
- 前端与算法 leetcode 283. 移动零
目录 # 前端与算法 leetcode 283. 移动零 题目描述 概要 提示 解析 解法一:暴力法 解法二:双指针法 算法 传入[0,1,0,3,12]的运行结果 执行结果 GitHub仓库 # 前 ...
- strGame:博弈论,trie
挺有意思的一道题.初探博弈论. 最好自己思考? 我们先考虑只有1轮游戏的情况. 这题明显要在字符串上一位一位地走,所以对字符串建立起trie. 最终建立起的trie的叶节点就是必败位置了. 对于非叶节 ...
- Redis实战--Redis整合SpringBoot示例
echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 该文章 ...
- Android DisplayMetrics 获取和屏幕相关的信息
Android源码中是这样来描述DisplayMetrics的. /** * A structure describing general information about a display, s ...
- 使用Typescript重构axios(十八)——请求取消功能:总体思路
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
- 原生JS封装_new函数,实现new关键字的功能
1.前言 众所周知:没有对象怎么办?那就new一个! 那么在JS中,当我们new一个对象的时候,这个new关键字内部都干了什么呢? 现在我们就来剖析一下原生JS中new关键字内部的工作原理. 2.原生 ...