Replies: 2 comments 5 replies
-
This code fragment e = bytearray(0xFF for _ in range(255*24)) is the economical approach. This creates an iterator which is much more efficient. |
Beta Was this translation helpful? Give feedback.
-
@cwalther @rkompass MicroPython doesn't know the length of the generator in this case, and so this will realloc the underlying buffer (many times!). However the realloc will always attempt to grow into adjacent unused heap without moving, so in many cases this will essentially be no more expensive than if it had done the full allocation. But in a case where the heap is a bit fragmented, this will be expensive. (MicroPython does have an optimisation here where if the generator does know its length then it does pre-allocate. For example |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Creating an initialised bytearray in what I understand to be a pythonic way uses significantly more memory. e.g.
Consumes around 30KB according to micropython.mem_info()
The 'anti-pattern':
Consumes around 6KB.
Both appear to have the same result. What am I doing wrong here ?
Beta Was this translation helpful? Give feedback.
All reactions