掌握Python系统管理-调试和分析脚本2- cProfile和timeit

简介: 调试和分析在Python开发中发挥着重要作用。 调试器可帮助程序员分析完整的代码。 调试器设置断点,而剖析器运行我们的代码,并给我们执行时间的详细信息。 分析器将识别程序中的瓶颈。我们将了解pdb Python调试器,cProfile模块和timeit模块来计算Python代码的执行时间。

调试和分析在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)

参考资料

分析和计时程序

分析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中的单元测试。我们将学习如何创建和使用单元测试。

相关文章
|
1天前
|
机器学习/深度学习 算法 vr&ar
PYTHON用时变马尔可夫区制转换(MARKOV REGIME SWITCHING)自回归模型分析经济时间序列
PYTHON用时变马尔可夫区制转换(MARKOV REGIME SWITCHING)自回归模型分析经济时间序列
12 4
|
1天前
|
机器学习/深度学习 算法 Python
数据分享|Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
数据分享|Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
14 4
|
2天前
|
机器学习/深度学习 算法 数据挖掘
PYTHON银行机器学习:回归、随机森林、KNN近邻、决策树、高斯朴素贝叶斯、支持向量机SVM分析营销活动数据|数据分享-2
PYTHON银行机器学习:回归、随机森林、KNN近邻、决策树、高斯朴素贝叶斯、支持向量机SVM分析营销活动数据|数据分享
21 1
|
6天前
|
存储 API Python
python之代理ip的配置与调试
python之代理ip的配置与调试
|
7天前
|
vr&ar Python
Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列4
Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列
30 0
|
7天前
|
机器学习/深度学习 算法 数据可视化
python用支持向量机回归(SVR)模型分析用电量预测电力消费
python用支持向量机回归(SVR)模型分析用电量预测电力消费
28 7
|
机器学习/深度学习 算法 Python
Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
20 0
|
7天前
|
算法 数据可视化 Python
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
13 0
|
Web App开发 存储
Python+selenium 自动化-操作已启用的chrome浏览器实例演示,chrome启用调试端口方法
Python+selenium 自动化-操作已启用的chrome浏览器实例演示,chrome启用调试端口方法
503 0
Python+selenium 自动化-操作已启用的chrome浏览器实例演示,chrome启用调试端口方法
|
NoSQL Python

热门文章

最新文章