gpt4 book ai didi

python - 为什么垃圾回收这么慢?

转载 作者:太空狗 更新时间:2023-10-29 21:58:17 26 4
gpt4 key购买 nike

使用 %prun 在 IPython 中分析我的代码,我注意到大部分函数时间花在垃圾收集上(0.334 秒对 0.428 总时间)。

79254 function calls (77408 primitive calls) in 0.428 seconds

Ordered by: internal time

ncalls tottime percall cumtime percall filename:lineno(function)
5 0.334 0.067 0.334 0.067 {gc.collect}
15757 0.005 0.000 0.007 0.000 {isinstance}
1584 0.002 0.000 0.004 0.000 dtypes.py:68(is_dtype)

我已经尝试在调用函数之前和返回其值之后禁用/启用垃圾收集,但时间几乎相同。

import gc

gc.disable()
x = foo()
gc.disable()

有谁知道为什么这是一个瓶颈以及如何加快速度?

我的 Python/Pandas 版本如下:

Python 2.7.11 |Continuum Analytics, Inc.| (default, Dec  6 2015, 18:57:58) 
Pandas 0.17.1

最佳答案

垃圾收集是许多现代语言的高级功能/抽象。它使程序变慢,但也使程序更不容易出错并且更容易创建。

这里有一些关于这个特定主题的好文章:

Python Garbage
Only slow if you use it wrong

关于python - 为什么垃圾回收这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com