python函数实验报告的简单介绍( 九 )


10272458 function calls (10272457 primitive calls) in 37.718 CPU secs
ncalls tottime percall cumtime percall filename:lineno(function)
10.000 0.000 37.718 37.718:1 ( )
10.719 0.719 37.717 37.717:12( )
1000 1.569 0.002 1.569 0.002:20(function_a)
1000 0.011 0.000 22.560 0.023:27(function_b)
5128000 7.078 0.000 7.078 0.000:28( )
1000 6.510 0.007 12.825 0.013:35(function_c)
5128000 6.316 0.000 6.316 0.000:36( )
在cProfile术语学中,原始调用指的就是非递归的函数调用 。
以这种方式使用cProfile模块对于识别值得进一步研究的区域是有用的 。比如,这里 我们可以清晰地看到function_b()需要耗费更长的时间 , 但是我们怎样获取进一步的详细资料?我们可以使用cProfile.run("function_b()")来替换对function_b()的调用 。或者可以保存完全的profile数据并使用pstats模块对其进行分析 。要保存profile,就必须对命令行进行稍许修改:python3 -m cProfile -o profileDataFile programOrModule.py 。之后可以对 profile 数据进行分析,比如启动IDLE,导入pstats模块 , 赋予其已保存的profileDataFile,或者也可以在控制台中交互式地使用pstats 。
下面给出的是一个非常短的控制台会话实例,为使其适合页面展示,进行了适当调整,我们自己的输入则以粗体展示:
$ python3 -m cProfile -o profile.dat MyModule.py
$ python3 -m pstats
Welcome to the profile statistics browser.
% read profile.dat
profile.dat% callers function_b
Random listing order was used
List reduced from 44 to 1 due to restriction
Function was called by...
ncalls tottime cumtime
:27(function_b) - 1000 0.011 22.251:12( )
profile.dat% callees function_b
Random listing order was used
List reduced from 44 to 1 due to restriction
Function called...
ncalls tottime cumtime
:27(function_b)-
1000 0.005 0.005 built-in method bisectJeft
1000 0.001 0.001 built-in method len
1000 1 5.297 22.234 built-in method sorted
profile.dat% quit
输入help可以获取命令列表,help后面跟随命令名可以获取该命令的更多信息 。比如, help stats将列出可以赋予stats命令的参数 。还有其他一些可用的工具,可以提供profile数据的图形化展示形式 , 比如 RunSnakeRun (), 该工具需要依赖于wxPython GUI库 。
使用timeit与cProfile模块,我们可以识别出我们自己代码中哪些区域会耗费超过预期的时间;使用cProfile模块,还可以准确算岀时间消耗在哪里 。
以上内容部分摘自视频课程 05后端编程Python-19调试、测试和性能调优(下),更多实操示例请参照视频讲解 。跟着张员外讲编程,学习更轻松,不花钱还能学习真本领 。
python函数实验报告的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、python函数实验报告的信息别忘了在本站进行查找喔 。

推荐阅读