【发布时间】:2020-08-10 04:38:41
【问题描述】:
我正在研究图形上的 python 函数,它是递归和 NP 复杂的。但是,我在很长一段时间内得到了结果,所以我使用了 kernprof 来查看哪些行花费的时间更多。
我看到这条线比我想象的要花更多时间:
Line # Hits Time Per Hit % Time Line Contents
==============================================================
...
53 120244 740797.0 6.2 17.0 if any( [m[passed].get(i, False) for passed in isom] ): # si ce noeud est lié à un noeud passé, le tester
所以我选择使用生成器,因为我似乎不应该生成所有列表,因为我只是感兴趣,如果有的话。
但是有了生成器,我得到了这个结果:
Line # Hits Time Per Hit % Time Line Contents
==============================================================
...
53 35283 509901.0 14.5 12.8 if any( (m[passed].get(i, False) for passed in isom) ): # si ce noeud est lié à un noeud passé, le tester
所以它似乎比使用列表理解更快,但我不明白为什么每次点击的时间更大,为什么我的点击次数少得多?
【问题讨论】:
标签: python performance generator