gpt4 book ai didi

python - 为什么这三种创建列表的方法在内存大小上存在差异?

转载 作者:太空狗 更新时间:2023-10-30 01:04:15 25 4
gpt4 key购买 nike

<分区>

sys.getsizeof(list(range(10))) # 200
sys.getsizeof([0,1,2,3,4,5,6,7,8,9]) # 144
sys.getsizeof([i for i in range(10)]) # 192

我的 C 经验很少,所以这可能超出了我的理解范围,但我很好奇,因为我正在使用 sys.getsizeof

我试图查看文档,但我只找到了这个:

getsizeof() calls the object’s sizeof method and adds an additional garbage collector overhead if the object is managed by the garbage collector.

由于我的 C 经验很少,我对 GC 也不太熟悉,但是从我与 Python 相关的 GC 阅读中,我了解到在 Python 中只计算引用。在上述情况下,我们没有将它保存到变量中,所以我假设没有 GC 引用?

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