我有一组python脚本,我想用kernprof profiler对其进行分析,但我也希望能够在正常执行过程中不使用kernprof来运行它。
在没有内核的情况下,在执行过程中忽略未定义的@profile的优雅方法是什么?或者任何其他装饰师。
示例代码:
@profile
def hello():
print('Testing')
hello()与以下人一起奔跑:
kernprof -l test.py正确地在@profile方法上执行分析器
与以下人一起奔跑:
python test.py 返回一个错误:
Traceback (most recent call last):
File "test.py", line 1, in <module>
@profile
NameError: name 'profile' is not defined希望避免在任何地方捕获此错误,因为我希望代码执行起来就像@profile在没有用kernprof调用时是一个无操作一样。
谢谢!-Laura
编辑:我最终使用了cProfile和kcache研磨,并且完全避免使用装饰器。
Using cProfile results with KCacheGrind
python -m cProfile -o profile_data.pyprof run_cli.py
pyprof2calltree -i profile_data.pyprof && qcachegrind profile_data.pyprof.log发布于 2015-10-28 19:53:28
如果未从kernprof执行,则定义无操作装饰器:
if 'profile' not in globals():
def profile(func):
return func发布于 2015-11-13 03:42:56
Daniel提出的方法的一个不同之处是使用以下一行代码,然后根据您对分析的需要对其进行输入和输出注释:
# Optional no-op decorator, comment when you want to profile
def profile(func): return funchttps://stackoverflow.com/questions/33400359
复制相似问题