On Fri, 2019-01-11 at 12:32 -0800, Keith Goodman wrote:
> I remember back when a.sum(axis=0) was much slower than a.sum(axis=1)
> for something like a=np.ones((1000, 1000)). But now it runs in about
> the same time. How does numpy do it?
> 

"now" is since numpy 1.7 or so :).

> Does numpy do something like
> 
> for i in range(a.shape[0]):
>     for j in range(x.shape[1]):
>         result[j] += a[i, j]

Yeah, numpy reorders the operation. Maybe a bit closer to what happens
is to write it down with the result as a 2D array (as happens with
keepdims), since internally that is how numpy operates on it (although
it may optimize the `i*0` away):

for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        # If sum is along axis 0:
        result[i*0, j] += a[i, j]

Since it doesn't matter which of the loop is the innermost one, the
machinery is capable of reordering them. I think it learned it with 1.7
(because that added a lot), but maybe it was even earlier.

- Sebastian


> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion

Reply via email to