[英]More memory-efficient struct representation in Python?
I have the equivalent of a classic Point
struct that I'm trying to create. 我具有要创建的经典
Point
结构。
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
However, I'll only need limited functionality (access by attribute name) and none of the extra overhead of namedtuples (eg length, index access, __contains__
etc.) Moreover, my use case also has fixed types for Point.x
and Point.y
so maybe there's a further hack that relies on the static typing guarantees. 不过,我只需要有限的功能(按属性名称访问),没有namedtuples的额外开销(如长度,索引访问的
__contains__
等)。此外,我的使用情况下,也有固定的类型Point.x
和Point.y
因此,也许还有更多依赖静态类型保证的技巧。
Is there something with even less memory overhead? 是否有一些内存开销更少的东西? Perhaps a
ctypes
or Cython
solution? 也许是
ctypes
或Cython
解决方案?
I guess, creating a Cython extension will be the easiest way to reduce memory impact. 我想,创建Cython扩展将是减少内存影响的最简单方法。 Attributes of Cython extension types are stored directly in the object's C struct and the set of attributes is fixed at compile time (much like Python's
__slots__
). Cython扩展类型的属性直接存储在对象的C结构中,并且属性集在编译时固定(与Python的
__slots__
相似)。
cdef class Point:
cdef readonly double x, y # C-level attributes
def __init__(self, double x, double y):
self.x = x
self.y = y
def __repr__(self):
return 'Point({}, {})'.format(self.x, self.y)
For the cases when Cython is not the option 对于无法使用Cython的情况
There is a way to reduce the memory footprint: 有一种减少内存占用的方法:
>>> from recordclass import dataobject
>>> class Point(dataobject):
... x:int
... y:int
>>>
>>> p = Point(1,2)
>>> class Point2(object):
.... __slots__ = ('x', 'y')
.... def __init__(self, x, y):
.... self.x = x
.... self.y = y
>>>
>>> p2 = Point2(1,2)
>>> from sys import getsizeof as sizeof
>>> sizeof(p2) - sizeof(p)
24
The difference is equal to the size of extra space used for cyclic garbage collection support. 差异等于用于循环垃圾收集支持的额外空间的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.