Strange array.array performance
Maxim Khitrov
mkhitrov at gmail.com
Thu Feb 19 21:08:43 EST 2009
On Thu, Feb 19, 2009 at 7:01 PM, Scott David Daniels
<Scott.Daniels at acm.org> wrote:
> Maxim Khitrov wrote:
>>
>> On Thu, Feb 19, 2009 at 2:35 PM, Robert Kern <robert.kern at gmail.com>
>> wrote:
>> I have, but numpy is not currently available for python 2.6, which is
>> what I need for some other features, and I'm trying to keep the
>> dependencies down in any case....
>> The only feature that I'm missing with array.array is the ability to
>> quickly pre-allocate large chunks of memory. To do that right now I'm
>> using array('d', (0,) * size). It would be nice if array accepted an
>> int as the second argument indicating how much memory to allocate and
>> initialize to 0.
>
> In the meantime, you could write a function (to ease the shift to numpy)
> and reduce your interface problem to a very small set of lines:
> def zeroes_d(n):
> '''Allocate a n-element vector of 'd' elements'''
> vector = array.array('d') # fromstring has no performance bug
> vector.fromstring(n * 8 * '\0')
> return vector
> Once numpy is up and running on 2.6, this should be easy to convert
> to a call to zeroes.
Here's the function that I'll be using from now on. It gives me
exactly the behavior I need, with an int initializer being treated as
array size. Still not as efficient as it could be if supported
natively by array (one malloc instead of two + memmove + extra
function call), but very good performance nevertheless:
from array import array as _array
array_null = dict((tc, '\0' * _array(tc).itemsize) for tc in 'cbBuhHiIlLfd')
def array(typecode, init):
if isinstance(init, int):
return _array(typecode, array_null[typecode] * init)
return _array(typecode, init)
- Max
More information about the Python-list
mailing list