How to increase the speed of this program?

Peter Otten __peter__ at web.de
Tue Nov 28 07:46:50 EST 2006


Fredrik Lundh wrote:

> John Machin wrote:
> 
>> I'm extremely agnostic about the spelling :-) IOW I'd be very glad of
>> any way [pure Python; e.g. maintaining my own version of the array
>> module doesn't qualify] to simply and rapidly create an array.array
>> instance with typecode t and number of elements n with each element
>> initialised to value v (default to be the zero appropriate to the
>> typecode).
> 
>      array(t, [v])*n

Of course Leo was already there before I messed it up again. 

$ python2.5 -m timeit -s'from array import array; s = "abc"' 'a = array("c",
s); a*1000000'
10 loops, best of 3: 53.5 msec per loop

$ python2.5 -m timeit -s'from array import array; s = "abc"' 'a = array("c",
s); s*1000000'
100 loops, best of 3: 7.63 msec per loop

So str * N is significantly faster than array * N even if the same amount of
data is copied.

Peter




More information about the Python-list mailing list