[Numpy-discussion] array vs. matrix performance

David Cournapeau david at ar.media.kyoto-u.ac.jp
Mon May 21 09:22:45 EDT 2007


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).

David



More information about the NumPy-Discussion mailing list