python空间占用

python空间占用,第1张

python空间占用

python2,python3空间占用
  • 前言
    • python2
    • python3

前言

最近使用python写了一个比较大的程序发现,不同的元素占用空间都很大,且python2和python3不一样,列举在下面

python2

空链表72字节,空字典280字节,空集合232字节,None16字节,char38字节, int绝对值在现在的ns时间戳范围内都是24字节;

Python 2.7.16 (default, Sep  6 2021, 07:39:44)
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getsizeof(1636943622)
24
>>> sys.getsizeof(1636943622000)
24
>>> sys.getsizeof(1636943622000000)
24
>>> sys.getsizeof(1636943622000000000)
24
>>> sys.getsizeof(1736943622000000000)
24
>>> sys.getsizeof(-1736943622000000000)
24
>>> sys.getsizeof([])
72
>>> sys.getsizeof(None)
16
>>> sys.getsizeof({})
280
>>> sys.getsizeof(set())
232
python3

空链表56字节,空字典64字节,空集合216字节,None16字节,char50字节,0是24字节,除0外常见的int为28字节,秒,毫秒,微秒级别的时间戳都是32字节,纳秒级别时间戳是36字节;

Python 3.9.6 (default, Jun 29 2021, 05:25:02)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getsizeof(0)
24
>>> sys.getsizeof(1)
28
>>> sys.getsizeof(-1)
28
>>> sys.getsizeof(1636943622)
32
>>> sys.getsizeof(1636943622000)
32
>>> sys.getsizeof(1636943622000000)
32
>>> sys.getsizeof(1636943622000000000)
36
>>> sys.getsizeof(-1636943622000000000)
36
>>> sys.getsizeof(-1736943622000000000)
36
>>> sys.getsizeof([])
56
>>> sys.getsizeof({})
64
>>> sys.getsizeof(set())
216
>>> sys.getsizeof(None)
16

python中不要轻易将默认值设成空list,空字典,空集合,而应是None,另外实在要节约空间,还是不要用python了吧

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5495815.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存