Numeric/Numarray equivalent to zip ?

George Sakkis gsakkis at rutgers.edu
Sun May 1 17:18:49 EDT 2005


"Peter Otten" <__peter__ at web.de> wrote:
> George Sakkis wrote:
>
> > Though not the fastest to execute; using concatenate instead of
> > initializing an array from a list [a,a] is more than 2,5 time
faster in
> > my system (~4.6 vs 11.8 usec per loop according to timeit.py), and
it's
> > not harder either.
>
> That surprises me. I would expect essentially the same amount of
> data-shuffling.

Here are some timing comparisons of four versions I tried. The first
three work on 1D arrays directly and the fourth on 2D row arrays (i.e.
shape (1,len(a))):

from Numeric import *

# 11.5 usec/loop
def ziparrays_1(*arrays):
    return array(arrays)

# 8.1 usec/loop
def ziparrays_2(*arrays):
    a = zeros((len(arrays),len(arrays[0])))
    for i in xrange(len(arrays)):
        a[i] = arrays[i]
    return a

# 13.6 usec/loop
def ziparrays_3(*arrays):
    return reshape(concatenate(arrays), (len(arrays),len(arrays[0])))

# 4.6 usec/loop
def ziparrays_4(*arrays):
    return concatenate(arrays)


So if one has the choice, it's better to start with 2D arrays instead
of 1D. Comparing versions 3 and 4, it's surprising that reshape takes
twice as much as concatenate.

George




More information about the Python-list mailing list