[Numpy-discussion] How long does it take to create an array?
Robert Kern
robert.kern@gmail....
Fri Feb 5 14:29:48 CST 2010
On Fri, Feb 5, 2010 at 12:26, Keith Goodman <kwgoodman@gmail.com> wrote:
> Why is the second method of converting a list of tuples to an array so
> much faster?
>
>>> x = range(500)
>>> x = [(z,) for z in x] # <-- e.g. output of a sql database
>>> x[:5]
> [(0,), (1,), (2,), (3,), (4,)]
>>>
>>> timeit np.array(x).reshape(-1) # <-- slow
> 1000 loops, best of 3: 832 us per loop
>>> timeit np.array([z[0] for z in x])
> 10000 loops, best of 3: 106 us per loop # <-- fast
When array() gets a sequence of sequences, it has to do more work to
figure out the appropriate shape.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list