0

Efficient initialisation of arrays?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Efficient initialisation of arrays?

by kevinjwalters on Sat May 16, 2020 1:54 pm

Is there an efficient way to initalise a large array in CircuitPython? It's common to see it initialised with a list but I don't think there's any magic optimisations going on here judging by the size mentioned in the MemoryError and this temporary list is created in full:

Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit CircuitPlayground Express with samd21g18
>>> import array
>>> big = array.array("B", [128] * 8000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError: memory allocation failed, allocating 32000 bytes


I can workaround this with a hacky doubling method using the array itself:

Code: Select all | TOGGLE FULL SIZE
>>> workaround = array.array("B", [0] * 1000)
>>> workaround.extend(workaround)
>>> workaround.extend(workaround)
>>> workaround.extend(workaround)
>>> len(workaround)
8000


Is there a better way to do that in Python / CircuitPython? It would be nice to be able to fill it with a specified value without allocating any memory for a same size data structure. ulab isn't an answer here as it's a CPX.

I've been playing around with a Talkie port and am currently using "B" arrays to keep memory usage low for RawSample.

kevinjwalters
 
Posts: 576
Joined: Sun Oct 01, 2017 3:15 pm

Re: Efficient initialisation of arrays?

by tannewt on Sun May 17, 2020 11:31 pm

Have you tried multiplying the array object itself? I *think* that may work. (If it doesn't, check CPython and if it does there we could add it.)

tannewt
 
Posts: 1639
Joined: Thu Oct 06, 2016 8:48 pm

Re: Efficient initialisation of arrays?

by kevinjwalters on Mon May 18, 2020 7:52 pm

Yep, it works well as long as you get them the right way around, thanks. I'll show both examples in case anyone tries it the wrong way around and searches for the error.

Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit CircuitPlayground Express with samd21g18
>>>
>>>
>>> import array
>>> buf = 16000 * array.array("B", [128])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported types for __mul__: 'int', 'array'
>>> buf = array.array("B", [128]) * 16000

kevinjwalters
 
Posts: 576
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.