简体   繁体   中英

More memory-efficient struct representation in Python?

I have the equivalent of a classic Point struct that I'm trying to create.

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.

Is there something with even less memory overhead? Perhaps a ctypes or Cython solution?

I guess, creating a Cython extension will be the easiest way to reduce memory impact. 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__ ).

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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