[Numpy-discussion] Slicing slower than matrix multiplication?

Jasper van de Gronde th.v.d.gronde@hccnet...
Fri Dec 11 05:49:10 CST 2009


(Resending without attachment as I don't think my previous message arrived.)

I just started using numpy and am very, very pleased with the
functionality and cleanness so far. However, I tried what I though would
be a simple optimization and found that the opposite was true.
Specifically, I had a loop where something like this was done:

    w += Xi[mu,:]
    E = np.dot(Xi,w)

Instead of repeatedly doing the matrix product I thought I'd do the
matrix product just once, before the loop, compute the product
np.dot(Xi,Xi.T) and then do:

    w += Xi[mu,:]
    E += Xi2[mu,:]

Seems like a clear winner, instead of doing a matrix multiplication it
simply has to sum two vectors (in-place). However, it turned out to be
1.5 times SLOWER...

I've attached a test file which shows the problem. It also tries adding
columns instead of rows (in case the memory layout is playing tricks),
but this seems to make no difference. This is the output I got:

    Dot product: 5.188786
    Add a row: 8.032767
    Add a column: 8.070953

Any ideas on why adding a row (or column) of a matrix is slower than
computing a matrix product with a similarly sized matrix... (Xi has less
columns than Xi2, but just as many rows.)



More information about the NumPy-Discussion mailing list