[Numpy-discussion] array vs. matrix performance

Charles R Harris charlesr.harris at gmail.com
Mon May 21 10:10:12 EDT 2007


On 5/21/07, David Cournapeau <david at ar.media.kyoto-u.ac.jp> wrote:
>
> Nils Wagner wrote:
> > Robert Cimrman wrote:
> >> I have come to a case where using a matrix would be easier than an
> >> array. The code uses lots of dot products, so I tested scipy.dot()
> >> performance with the code below and found that the array version is
> much
> >> faster (about 3 times for the given shape). What is the reason for
> this?
> >> Or is something wrong with my measurement?
> >>
> >> regards,
> >> r.
> >>
> >> ---
> >> import timeit
> >> setup = """
> >> import numpy as nm
> >> import scipy as sc
> >> X = nm.random.rand( 100, 3 )
> >> X = nm.asmatrix( X ) # (un)comment this line.
> >> print X.shape
> >> """
> >> tt = timeit.Timer( 'sc.dot( X.T, X )', setup )
> >> print tt.timeit()
> >> _______________________________________________
> >> Numpy-discussion mailing list
> >> Numpy-discussion at scipy.org
> >> http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >>
> > Confirmed, but for what reason ?
> >
> > 0.5.3.dev3020
> > 1.0.3.dev3792
> > Array version
> > 6.84843301773
> > Matrix version
> > 17.1273219585
> >
> My guess would be that for such small matrices, the cost of matrix
> wrapping is not negligeable against the actual computation. This
> difference disappears for bigger matrices (for example, try 1000 and
> 5000 instead of 100 for the first dimension).


Hmmm, I wonder how Tim's matrix version works. I've attached the code. I am
rather fond of the approach and have used it a few times myself. Tim uses
the dot product is written so: a(b). That is, the () operator is used
instead of the *.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070521/72b7db2b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mat.py
Type: text/x-python
Size: 3604 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070521/72b7db2b/attachment.py>


More information about the NumPy-Discussion mailing list