summation along a non-fast axis

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

summation along a non-fast axis

Keith Goodman
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?

Does numpy do something like

for i in range(a.shape[0]):
    for j in range(x.shape[1]):
        result[j] += a[i, j]

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: summation along a non-fast axis

Sebastian Berg
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
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

signature.asc (849 bytes) Download Attachment