[英]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.