简体   繁体   English

使用 spyder 进行 Python 分析:成分的时间总和不等于函数的总时间

[英]Python profiling with spyder: times for constituents do not sum up to a total function's time

I am profiling my python code using Spyder IDE.我正在使用 Spyder IDE 分析我的 python 代码。

As you can see in the screenshot below, it took 1.20 min = 80 sec for the _get_loglik_seq function to execute.正如您在下面的屏幕截图中看到的那样, _get_loglik_seq函数的执行时间为 1.20 分钟 = 80 秒。 However, the timing of various functions/procedures that constitutes this function are the following: solve_EV = 29.78s, c_get_gamma = 10.12 sec, norm = 6.57 sec, outer = 4.70 sec, <method dot ... : 2.17 sec, all others: <1 sec.然而,构成该函数的各种函数/过程的时序如下: solve_EV = 29.78s, c_get_gamma = 10.12 sec, norm = 6.57 sec, outer = 4.70 sec, <method dot ... : 2.17 sec, all c_get_gamma : <1 秒。

If one sums up these times, the result will be about 54 s, which is way smaller than 80s.如果将这些时间相加,结果将是大约 54 秒,这远小于 80 秒。

How is it possible?这怎么可能? Is it a profiler bug or did I miss something?这是分析器错误还是我错过了什么? Can anyone suggest a good tool for Python code profiling?任何人都可以推荐一个用于 Python 代码分析的好工具吗? (I am working on Mac OS) (我在 Mac OS 上工作)

Thanks, Mikhail谢谢,米哈伊尔

在此处输入图片说明

The reason is the following:原因如下:

Total time = execution time of the function, including all the sub-functions.总时间 = 函数的执行时间,包括所有子函数。

Local time = execution time of the function without it's sub-functions.本地时间 = 没有子函数的函数的执行时间。

Therefore, if a function p calls subfunctions s1 , s2 , s3 , then:因此,如果函数p调用子函数s1s2s3 ,则:

ToTtime(p)=ToTtime(s1)+ToTtime(s2)+ToTtime(s3)+Localtime(p),

which approximately holds.大致成立。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM