我有一个在Linux上运行的C++应用程序,我正在优化它.如何确定代码的哪些区域运行缓慢?
我正在使用cProfile来配置我的Python程序.根据这个说法,我的印象是KCacheGrind可以解析并显示cProfile的输出.
但是,当我去导入文件时,KCacheGrind只会在状态栏中显示"未知文件格式"错误,并且不显示任何内容.
在我的性能分析统计数据与KCacheGrind兼容之前,我需要做些什么特别的事情吗?
...
if profile:
import cProfile
profileFileName = 'Profiles/pythonray_' + time.strftime('%Y%m%d_%H%M%S') + '.profile'
profile = cProfile.Profile()
profile.run('pilImage = camera.render(scene, samplePattern)')
profile.dump_stats(profileFileName)
profile.print_stats()
else:
pilImage = camera.render(scene, samplePattern)
...
Run Code Online (Sandbox Code Playgroud)
包版本
我想创建一个装饰器,用于描述方法并记录结果.如何才能做到这一点?
我正在研究 python,并遇到了一些查找代码的统计信息和执行时间的概念
假设我有以下代码
from time import gmtime, strftime
import timeit
def calculation():
a = 2
b = 3
res = a + b
return res
if 'name' == 'main' :
exec_time = timeit.timeit(calculation)
print exec_time
Run Code Online (Sandbox Code Playgroud)
结果:
0.2561519145965576
Run Code Online (Sandbox Code Playgroud)
所以从上面的代码我可以找到代码的执行时间,但是如何在python中找到代码的统计信息呢?
最后我的意图如下
编辑后的代码:
例如我的文件中有上面的代码test.py
现在我已经使用下面的命令运行了上面的文件
python -m cProfile test.py
Run Code Online (Sandbox Code Playgroud)
结果 :
sh-4.2$ python -m cProfile test.py
4 function calls in 0.001 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.001 0.001 0.001 0.001 test.py:1(<module>)
1 …Run Code Online (Sandbox Code Playgroud) 我想知道是否有类似Java的VisualVM的Python.我需要的功能是对正在运行的Python解释器进行实时分析.
一点背景:我最近对代码库的更改大大减慢了它的速度.我想知道我的代码在做什么而不必重新开始计算.另一种方法是停止解释器并重新启动它-m cProfile,从而减少一天的CPU时间.
任何建议都非常感谢.
profiling ×4
python ×4
c++ ×1
cprofile ×1
decorator ×1
kcachegrind ×1
statistics ×1
unix ×1
visualvm ×1