内存紧凑设计
您的任务显然是针对一些2B-values 的内存轻量级占用。
Python 中的解决方案还有一些需要考虑的因素。
假设值的范围,满足在int-s 的2B-范围内表示的值范围(数字范围0..65535)具有内存效率只是一种错觉。
您的设计应至少考虑与对象的“internal_representation”相关的开销 + 与对象实例的 相关的开销访问方法,以获得“那里”和“回来”的数字(不是说性能影响,因为您的要求集中在低内存占用,但更智能的是代理数据结构,更大的数据结构访问/维护 CPU 成本通常会增加)。
如果您的主要动机是设计低内存占用空间,那么最坏的情况将总结所有这些静态内存分配 + 访问方法期间请求的任何动态分配的要求对象修改操作 + import-ed 模块的大小。
一旦谈到 numpy - 一个很棒的工具 - 您将获得一个强大的库,用于快速、矢量化数组操作,并具有给定的单元数据表示 (dtype = numpy.uint8)。
隐藏部分(对于代码设计低内存占用的任何假设)是考虑该方法的整体内存成本。
不仅要比较对象大小,还要比较与类相关的开销(为清楚起见,没有显示大量 numpy 方法):
>>> anIntOBJECT= 8
>>> anIntOBJECT.__sizeof__() # ____________________________instance-method works
12
>>> sys.getsizeof(anIntOBJECT) # kindly do not modify the MCVE with erroneous code
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> dir(anIntOBJECT)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
>>> len( dir( anIntOBJECT ) ) # Class-methods
64
另一种基于字符串的数据表示方法:
>>> aStrOBJECT = "08"
>>> aStrOBJECT.__sizeof__() # ____________________________instance-method works
23
>>> sys.getsizeof(aStrOBJECT) # kindly do not modify the MCVE with erroneous code
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> dir( aStrOBJECT )
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> len( dir( aStrOBJECT ) )
71
可变序列的另一个主要原型:
>>> aBytearrayOfINTs = bytearray() # in python base
>>> aBytearrayOfINTs.__sizeof__() # _________________________instance-method works
24
>>> dir( aBytearrayOfINTs )
['__add__', '__alloc__', '__class__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'capitalize', 'center', 'count', 'decode', 'endswith', 'expandtabs', 'extend', 'find', 'fromhex', 'index', 'insert', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'pop', 'remove', 'replace', 'reverse', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> len( dir( aBytearrayOfINTs ) )
76
真正的数据结构实现:访问和维护方法决定
在所有情况下,数据结构访问/维护方法将是一个决定性因素,可能不仅仅是单值(最小表示)内存占用量预期。