Is there a difference (in performance for example) when comparing shape
and len
? Consider the following example:
In [1]: import numpy as np
In [2]: a = np.array([1,2,3,4])
In [3]: a.shape
Out[3]: (4,)
In [4]: len(a)
Out[4]: 4
Quick runtime comparison suggests that there's no difference:
In [17]: a = np.random.randint(0,10000, size=1000000)
In [18]: %time a.shape
CPU times: user 6 µs, sys: 2 µs, total: 8 µs
Wall time: 13.1 µs
Out[18]: (1000000,)
In [19]: %time len(a)
CPU times: user 5 µs, sys: 1 µs, total: 6 µs
Wall time: 9.06 µs
Out[19]: 1000000
So, what is the difference and which one is more pythonic? (I guess using shape
).
I wouldn't worry about performance here - any differences should only be very marginal.
I'd say the more pythonic alternative is probably the one which matches your needs more closely:
a.shape
may contain more information than len(a)
since it contains the size along all axes whereas len
only returns the size along the first axis:
>>> a = np.array([[1,2,3,4], [1,2,3,4]])
>>> len(a)
2
>>> a.shape
(2L, 4L)
If you actually happen to work with one-dimensional arrays only, than I'd personally favour using len(a)
in case you explicitly need the array's size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With