efficiently create and fill array.array from C code?

Thomas Jollans thomas at jollans.com
Mon Jun 14 08:28:30 EDT 2010


On 06/14/2010 01:18 PM, Hrvoje Niksic wrote:
> Thomas Jollans <thomas at jollans.com> writes:
> 
>> 1. allocate a buffer of a certain size
>> 2. fill it
>> 3. return it as an array.
> 
> The fastest and more robust approach (I'm aware of) is to use the
> array.array('typecode', [0]) * size idiom to efficiently preallocate the
> array, and then to get hold of the pointer pointing into array data
> using the buffer interface.

Ah, create a single-element array, and multiply that. That's not a bad
approach, the overhead is probably equivalent to what I have now:
currently, I create an uninitialized(!) bytes of the correct size, fill
it myself, and initialize an array from that. Both approaches have the
overhead of creating one extra Python object (bytes/single-element
array) and either copying one element over and over, or memcpy'ing the
whole buffer.

> 
> Please send a message to capi-sig at python.org, a SIG specializing in the
> Python/C API, if you need more help with implementing this.

I'll probably subscribe to that list, thanks for the hint.

-- Thomas



More information about the Python-list mailing list