[Numpy-discussion] speed of array creation from tuples

Keith Goodman kwgoodman@gmail....
Mon Feb 27 17:12:45 CST 2012


On Mon, Feb 27, 2012 at 2:55 PM, Skipper Seabold <jsseabold@gmail.com> wrote:
> I am surprised by this (though maybe I shouldn't be?) It's always faster to
> use list comprehension to unpack lists of tuples than np.array/asarray?
>
> [~/]
> [1]: X = [tuple(np.random.randint(10,size=2)) for _ in
> range(100)]
>
> [~/]
> [2]: timeit np.array([x1 for _,x1 in
> X])
> 10000 loops, best of 3: 26.4 us per loop
>
> [~/]
> [3]: timeit
> np.array(X)
> 1000 loops, best of 3: 378 us per loop
>
> [~/]
> [4]: timeit x1, x2 = np.array([x for _,x in X]), np.array([y for y,_ in
> X])
> 10000 loops, best of 3: 53.4 us per loop
>
> [~/]
> [5]: timeit x1, x2 =
> np.array(X).T
> 1000 loops, best of 3: 384 us per loop

Here's a similar thread:
http://mail.scipy.org/pipermail/numpy-discussion/2010-February/048304.html


More information about the NumPy-Discussion mailing list