python怎么使用__slots__让你的代码更加节省内存

  介绍

小编给大家分享一下python怎么使用__slots__让你的代码更加节省内存,希望大家阅读完这篇文章之后都有所收获、下面让我们一起去探讨吧!

<强>现在来说说python中dict为什么比列表浪费内存?

和列表相比,dict查找和插入的速度极快,不会随着关键的增加而增加,dict需要占用大量的内存,内存浪费多。

而名单查找和插入的时间随着元素的增加而增加,占用空间,小浪费的内存很少。

python解释器是Cpython的,这两个数据结构应该对应C的哈希表和数组。因为哈希表需要额外内存记录映射关系,而数组只需要通过索引就能计算出下一个节点的位置,所以哈希表占用的内存比数组大,也就是dict比列占用的内存更大。

如果想更加详细了解,可以查看C的源代码.python官方链接:https://www.python.org/downloads/source/

<>强如下代码是我从python官方截取的代码片段:

<强>列表源码:

typedef  struct  {   ,PyObject_VAR_HEAD   ,/* Vector  of  pointers 用list 元素只列出[0],is  ob_item[0],等只*/,PyObject  * * ob_item;   ,   ,/* ob_item  contains  space  for  & # 39;分配# 39;元素又是;从而数量   ,* currently  use  is  ob_size拷贝。   ,*不变量:   ,* 0,& lt;=, ob_size  & lt;=分配   ,* len(列表),==ob_size   ,* ob_item ==, NULL  implies  ob_size ==, allocated ==0   list . sort()就,*,temporarily  sets  allocated 用1,用detect 突变。   ,*   ,* Items  must  normally  not  be  NULL, except  during  construction 当   ,*,list  is  not  yet  visible  outside 从而function  that  builds 它。   ,*/,Py_ssize_t 分配;   },PyListObject;

<强> Dict源码:

/*, PyDict_MINSIZE  is 从而minimum  size  of  a 字典只却;能够many  slots    ,* allocated  directly 拷贝,dict  object (从而拷贝;ma_smalltable 成员)。   ,* It  must  be  a  power  of  2,以及at  least  4只;8,allows  dicts  with  no 更多   ,* than  5, active  entries 用live 拷贝ma_smalltable (以及so  avoid 一个   ,* additional  malloc);, instrumentation  suggested 却;能够suffices  for    ,* majority  of  dicts  (consisting  mostly  of  usually-small  instance  dicts 和   ,* usually-small  dicts  created 用pass  keyword 参数)。   ,*/# define  PyDict_MINSIZE  8   ,   typedef  struct  {   ,/* Cached  hash  code  of  me_key只Note  that  hash  codes 断开连接;C 多头。   ,* have 用我方表示歉意use  Py_ssize_t  instead  because  dict_popitem(),虐待   ,* me_hash 用于a  search 手指。   ,*/,Py_ssize_t  me_hash;   ,PyObject  * me_key;   ,PyObject  * me_value;   },PyDictEntry;   ,/*   用ensure 从而lookup  algorithm 终止,,there  must  be  at  least  one 未使用   slot  (NULL 键),拷贝,表。   从而value  ma_fill  is 从而number  of  non-NULL  keys  (sum  of  Active 以及假);   ma_used  is 从而number  of 非空,,non-dummy  keys  (==,, number  of 非空==values 从而,number  of  Active 项目)。   用avoid  slowing  down  lookups 提醒a  near-full 表,,resize 从而table 当我方表示歉意   这# 39;s  two-thirds 满。   */typedef  struct  _dictobject  PyDictObject;   struct  _dictobject  {   ,PyObject_HEAD   ,Py_ssize_t  ma_fill;/*, #, Active  +, # Dummy  */,Py_ssize_t  ma_used;/*, # Active  */,   ,/*,table  contains  ma_mask  +, 1,槽,以及这# 39;s  a  power  of  2。   ,* store 我方表示歉意,mask  instead  of 从而size  because 从而mask  is 更多   ,* frequently 需要。   ,*/,Py_ssize_t  ma_mask;   ,   ,/* ma_table  points 用ma_smalltable  for  small 表、else    ,* additional  malloc # 39; ed 记忆只ma_table  is  never 零!却;能够统治   ,* saves  repeated  runtime  null-tests 拷贝,workhorse  getitem    ,* setitem 调用。   ,*/,PyDictEntry  * ma_table;   ,PyDictEntry  * (* ma_lookup) (PyDictObject  *议员,PyObject  *键,long 散列);   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

python怎么使用__slots__让你的代码更加节省内存