[英]cProfile, how to understand why lambda function is most time consuming call
我是 cprofile 的新手,但我在为 ArcGIS 编写的 python 工具上运行它。 它告诉我最耗时的函数是 _base.py 中的 lambda 函数:
通话 | 总时间 | 电话 | 业余时间 | 电话 | 文件名:lineno(函数) |
---|---|---|---|---|---|
17875 | 137.877 | 0.008 | 200.404 | 0.011 | _base.py:510(<lambda>) |
962732 | 67.568 | 0 | 67.568 | 0 | {built-in method createobject} |
7497 | 44.223 | 0.006 | 44.223 | 0.006 | {built-in method distanceto} |
8301909/8300996 | 35.652 | 0 | 35.659 | 0 | {getattr} |
713 | 35.235 | 0.049 | 35.235 | 0.049 | {built-in method listfields} |
_base.py 是此位置的 arcpy 文件:
C:\Program Files (x86)\ArcGIS\Desktop10.8\arcpy\arcpy\geoprocessing
如何更深入地挖掘并找出使用此 lambda 函数的内容?
该工具有超过 5000 行代码,所以我无法发布它,但我在一开始就导入了 cprofile:
import cProfile, pstats
profiler = cProfile.Profile()
profiler.enable()
然后禁用它并在结束前转储统计信息:
profiler.disable()
stats.dump_stats(myStatsLocation)
通常当我需要调试慢代码时,我会使用line profiler 。 使用它,您可以在可疑的慢功能上使用装饰器。 从那里,您可以像使用 cProfiler 一样找出调用了什么以及调用了多少次。
cProfiler 已经精确定位到您的_base.py
中的第 510 行。 所以可能那个 lambda 调用了另一个很慢的函数。 在该函数上添加@profile
可能会对您的性能问题有所了解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.