3 ms·
I did a similar exercise on a Python program a few years ago now. In the end I ended up adding __slots__ to two classes and the memory usage shrunk by 2/3 (the
by nickcw 6y ago
I did a similar exercise on a Python program a few years ago now.
In the end I ended up adding __slots__ to two classes and the memory usage shrunk by 2/3 (there were a lot of those two objects).
I also managed to double the speed by re-writing (re-wording really) a bit of string handling which just happened to be the bottleneck. Can't remember the details now but it was a trivial code change.
Profiling for the win!
- im3w1l 6y agoI would assume (but haven't tested) that if you have a small class with a lot of instances, that you could make it even more efficient by doing the "Array of Structures" to "Structure of Arrays" transformation[0]. Since with a structure of arrays, you could use numpy, and not have object overhead for every integer or float or bool or whatever it is you are storing. https://en.wikipedia.org/wiki/AoS_and_SoA https://en.wikipedia.org/wiki/AoS_and_SoA