简体   繁体   English

使用由另一个 function 调用的线路分析 function 时出错

[英]Error when using line profiling a function that's called by another function

I am using lprun to do line profiling for my code in Jupyterlab.我正在使用lprun对我在 Jupyterlab 中的代码进行行分析。 The structure of my code and the way I use lprun is as below.我的代码结构和我使用 lprun 的方式如下。

def fn1(a,b,c,d):
    ...
    y=fn2(a,c)  #is ok

    # but %lprun -f fn2 y=fn2(a,c) gives error

def fn2(a,c):
    .....
    .....

my_main_code lines
 ....
 %lprun -f fn1 x=fn1(a,b,c,d)
 .....

As you see, my main code calls fn1 and it calls fn2 .如您所见,我的主要代码调用fn1并调用fn2 When I use lprun just for fn1 it works well and I saw that fn2 takes a lot of time.当我仅对fn1使用lprun时效果很好,我看到fn2需要很多时间。 So, I decided to put lprun command on the fn2 call as shown above as a comment.因此,我决定将 lprun 命令放在fn2调用上,如上所示作为注释。 But it then throws an error that name c is not defined.但它随后会抛出一个错误,指出名称c未定义。 So, how can I do line profiling on fn2 ?那么,如何在fn2上进行线路分析?

I found a solution to this on my own!我自己找到了解决方案! To make the profiler display the time statistics for fn2 also, instead of original %lprun -f fn1 x=fn1(a,b,c,d) replace with %lprun -f fn2 -f fn1 x=fn1(a,b,c,d) .为了使探查器也显示fn2的时间统计信息,而不是原来的%lprun -f fn1 x=fn1(a,b,c,d)替换为%lprun -f fn2 -f fn1 x=fn1(a,b,c,d)

If you put lprun inside fn1 while calling fn2 , it throws the error as in question.如果您在调用fn2时将fn1放在lprun内,它会抛出所讨论的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python中的分析:谁调用了函数? - Profiling in Python: Who called the function? 用Cython进行线性分析内部功能 - Line Profiling inner function with Cython 为什么不能在 function 以及它调用的另一个 function 中调用 Python 的 yield 语句,为什么在尝试时没有报告错误? - Why can Python's yield statement not be called in a function and also in another function that it calls and why no error reported when attempted? 在调用另一个函数时运行一个函数 - Running a function when another function is called Python分析:在每行函数上花费的时间 - Python profiling: time spent on each line of function 检查是否可以使用其他函数的参数调用函数 - Check if function can be called with another function's arguments 从函数调用常量时,(PEP8)换行的正确方法是什么? - What's the proper way to (PEP8)line break for when constants are called from a function? 用于打印当前模块的名称,也可以从另一个模块调用 - function to print current module's name, also when called from another module Python分析:使用line_profiler的@profile装饰器会导致错误 - Python profiling: using line_profiler's @profile decorator results in error 函数只能在python shell中工作,但是不能在调用另一个函数时工作吗? - Function works in python shell alone, but not when called to another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM