python编写浪费内存吗_Python中dict为什么比list浪费内存?求大神,感激不尽!!...

展开全部

可以e69da5e6ba9062616964757a686964616f31333337376432翻阅python源码:

https://www.python.org/downloads/source/

List:typedef struct {

PyObject_VAR_HEAD

/* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */

PyObject **ob_item;

/* ob_item contains space for 'allocated' elements.  The number

* currently in use is ob_size.

* Invariants:

*     0 <= ob_size <= allocated

*     len(list) == ob_size

*     ob_item == NULL implies ob_size == allocated == 0

* list.sort() temporarily sets allocated to -1 to detect mutations.

*

* Items must normally not be NULL, except during construction when

* the list is not yet visible outside the function that builds it.

*/

Py_ssize_t allocated;

} PyListObject;

Dict:/* PyDict_MINSIZE is the minimum size of a dictionary.  This many slots are

* allocated directly in the dict object (in the ma_smalltable member).

* It must be a power of 2, and at least 4.  8 allows dicts with no more

* than 5 active entries to live in ma_smalltable (and so avoid an

* additional malloc); instrumentation suggested this suffices for the

* majority of dicts (consisting mostly of usually-small instance dicts and

* usually-small dicts created to pass keyword arguments).

*/

#define PyDict_MINSIZE 8

typedef struct {

/* Cached hash code of me_key.  Note that hash codes are C longs.

* We have to use Py_ssize_t instead because dict_popitem() abuses

* me_hash to hold a search finger.

*/

Py_ssize_t me_hash;

PyObject *me_key;

PyObject *me_value;

} PyDictEntry;

/*

To ensure the lookup algorithm terminates, there must be at least one Unused

slot (NULL key) in the table.

The value ma_fill is the number of non-NULL keys (sum of Active and Dummy);

ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL

values == the number of Active items).

To avoid slowing down lookups on a near-full table, we resize the table when

it's two-thirds full.

*/

typedef struct _dictobject PyDictObject;

struct _dictobject {

PyObject_HEAD

Py_ssize_t ma_fill;  /* # Active + # Dummy */

Py_ssize_t ma_used;  /* # Active */

/* The table contains ma_mask + 1 slots, and that's a power of 2.

* We store the mask instead of the size because the mask is more

* frequently needed.

*/

Py_ssize_t ma_mask;

/* ma_table points to ma_smalltable for small tables, else to

* additional malloc'ed memory.  ma_table is never NULL!  This rule

* saves repeated runtime null-tests in the workhorse getitem and

* setitem calls.

*/

PyDictEntry *ma_table;

PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, long hash);

PyDictEntry ma_smalltable[PyDict_MINSIZE];

};

PyObject_HEAD:#ifdef Py_TRACE_REFS

/* Define pointers to support a doubly-linked list of all live heap objects. */

#define _PyObject_HEAD_EXTRA            \

struct _object *_ob_next;           \

struct _object *_ob_prev;

#define _PyObject_EXTRA_INIT 0, 0,

#else

#define _PyObject_HEAD_EXTRA

#define _PyObject_EXTRA_INIT

#endif

/* PyObject_HEAD defines the initial segment of every PyObject. */

#define PyObject_HEAD                   \

_PyObject_HEAD_EXTRA                \

Py_ssize_t ob_refcnt;               \

struct _typeobject *ob_type;

PyObject_VAR_HEAD:/* PyObject_VAR_HEAD defines the initial segment of all variable-size

* container objects.  These end with a declaration of an array with 1

* element, but enough space is malloc'ed so that the array actually

* has room for ob_size elements.  Note that ob_size is an element count,

* not necessarily a byte count.

*/

#define PyObject_VAR_HEAD               \

PyObject_HEAD                       \

Py_ssize_t ob_size; /* Number of items in variable part */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值