[Numpy-discussion] Slicing slower than matrix multiplication?

Dag Sverre Seljebotn dagss@student.matnat.uio...
Fri Dec 11 06:01:18 CST 2009

Jasper van de Gronde wrote:
> (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.)
I think we need some numbers to put this into context -- how big are the 
vectors/matrices? How many iterations was the loop run? If the vectors 
are small and the loop is run many times, how fast the operation "ought" 
to be is irrelevant as it would drown in Python overhead.

Dag Sverre

More information about the NumPy-Discussion mailing list