繁体   English   中英

Python中更具内存效率的结构表示形式?

[英]More memory-efficient struct representation in Python?

我具有要创建的经典Point结构。

from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])

不过,我只需要有限的功能(按属性名称访问),没有namedtuples的额外开销(如长度,索引访问的__contains__等)。此外,我的使用情况下,也有固定的类型Point.xPoint.y因此,也许还有更多依赖静态类型保证的技巧。

是否有一些内存开销更少的东西? 也许是ctypesCython解决方案?

我想,创建Cython扩展将是减少内存影响的最简单方法。 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)

对于无法使用Cython的情况

有一种减少内存占用的方法:

>>> 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

差异等于用于循环垃圾收集支持的额外空间的大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM