For example classes have dicts for their methods and attributes.
If I remind right, a dict has a size of 112 bytes (since Python 3.6).
If you have only 10-1000 objects, you just don't care.
But with one million objects, you care about memory consumption.
In this case, use
If you have a for-loop, try to solve this not in Python.
Looping is not very efficient in Python. You can do this in cython.
Maybe you can post some example code with example data.
EDIT: Very often constructs like a list comprehension are faster than using generators. Generators are used, if you have much data to process and want to solve this in a iterative way and saving memory consumption. Maybe you can also implement a generator in Cython, which should run faster and saves memory.
If I remind right, a dict has a size of 112 bytes (since Python 3.6).
If you have only 10-1000 objects, you just don't care.
But with one million objects, you care about memory consumption.
In this case, use
__slots__
for this kind of classes.If you have a for-loop, try to solve this not in Python.
Looping is not very efficient in Python. You can do this in cython.
Maybe you can post some example code with example data.
EDIT: Very often constructs like a list comprehension are faster than using generators. Generators are used, if you have much data to process and want to solve this in a iterative way and saving memory consumption. Maybe you can also implement a generator in Cython, which should run faster and saves memory.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!