[Numpy-discussion] speed of array creation from tuples

Skipper Seabold jsseabold at gmail.com
Mon Feb 27 17:55:29 EST 2012


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


Skipper
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120227/9f08dae1/attachment.html>


More information about the NumPy-Discussion mailing list