这个 Python cProfile 输出是什么意思?
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 65.417 65.417 <string>:1(<module>)
1 43.675 43.675 65.417 65.417 primenumber_o.py:3(main)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
99999 21.742 0.000 21.742 0.000 {range}
2 0.000 0.000 0.000 0.000 {time.time}
具体来说,第三行。我阅读了 cProfile,但没有解释该行的含义。它也没有提供任何我可以在谷歌上搜索的关键字,所以我很困惑。
我正在分析的 Python 脚本查找素数。
我看到第 5 行的循环花费了 21 秒。我不明白的是另外 43 秒在做什么。
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 65.417 65.417 <string>:1(<module>)
1 43.675 43.675 65.417 65.417 primenumber_o.py:3(main)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
99999 21.742 0.000 21.742 0.000 {range}
2 0.000 0.000 0.000 0.000 {time.time}
Specifically, the third line. I read up on cProfile, but nothing explained what that line means. It also doesn't provide any keywords I could search for on Google, so I'm stumped.
The Python script I'm profiling finds prime numbers.
I see that 21 seconds is spent in the loop, on line 5. What I don't understand is what the other 43 seconds is doing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第三行是文件中除
range()
之外的所有代码。The third line is all of the code in the file that isn't
range()
.我没有使用过 cProfile,但在我看来,它根据函数名称将运行时分配到存储桶中。
for
语句中的内置函数range()
使用了 21 秒。另外 43 秒不在单独的命名函数中,因此它被认为属于 main 的名称,如下所示:如果将循环体放入函数中,则 43秒(或大部分)将显示在那里。如果将主体分成多个功能,您将获得更细粒度的分析。
I haven't used cProfile, but it looks to me that it assigns runtime into buckets based on function names. 21 seconds is being used by the built-in function
range()
in thefor
statement. The other 43 seconds isn't in a separate, named function, so the name it is considered to fall within ismain
, as in:If you put the body of the loop into a function, the 43 seconds (or most of it) will show up there. If you split up the body into multiple functions, you'll get more fine-grained profiling.