As a first step, you should use a NumPy array to store your data instead of a Python list.
As you rightly noted, the Python float uses internal double precision, and the double precision value that underlies floating Python can be represented in 8 bytes. But on a 64-bit machine with CPython for Python, a Python float takes up a full 24 bytes of memory: 8 bytes for a double-precision base value, 8 bytes for a pointer to the type of object, and 8 bytes for reference counting (used to collect garbage). In Python, there is no equivalent to the βprimitiveβ Java types or the .NET βvalueβ types β all in a box. This makes the semantics of the language simpler, but means that objects tend to be thicker.
Now, if we create a list of float objects in Python, the additional overhead of the list itself is added: one 8-byte pointer to an object in Python float (a 64-bit machine is still assumed here). Thus, in the general case, a list of n Python float objects will cost you more than 32n bytes of memory. On a 32-bit machine, everything is a little better, but not so much: our float objects will accept 16 bytes each, and we will use list pointers of 20n bytes of memory for a float list of length n . (Caveat: this analysis does not quite work if your list refers to the same Python float object from multiple list indices, but this is not a particularly common case.)
In contrast, an NumPy array of double precision n floats (using the NumPy float64 dtype) stores its data in a "packed" format in a single data block of 8n bytes, so taking into account the metadata of the array, the total memory requirement will be slightly less than 8n bytes.
Conclusion: simply by moving from a Python list to a NumPy array, you will reduce memory requirements by about 4 times. If this is still not enough, then it might make sense to consider reducing the accuracy from double to one precision (NumPy float32 dtype), if that matches your accuracy. NumPy float16 data type takes only 2 bytes per float, but writes only about three decimal digits of accuracy; I suspect that it will be almost useless for the application you are describing.
source share