Speed of matrix multiplication

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

Speed of matrix multiplication

Keith Goodman
Why is a.T*b slower than M.dot(a.T, b)? Does it take longer to parse
or something?

>> a = M.randn(500,1)
>> b = M.randn(500,1)
>> timeit a.T*b
100000 loops, best of 3: 11 µs per loop
>> timeit M.dot(a.T, b)
100000 loops, best of 3: 6.72 µs per loop

>> a = M.randn(5,1)
>> b = M.randn(5,1)
>> timeit a.T*b
100000 loops, best of 3: 10.1 µs per loop
>> timeit M.dot(a.T, b)
100000 loops, best of 3: 6.16 µs per loop

I use matrices, but for inner loops I'll convert to arrays:

>> a = a.A
>> b = b.A
>> dot = M.dot
>> timeit dot(a.T, b)
1000000 loops, best of 3: 1.82 µs per loop
>> a = a.T
>> timeit dot(a, b)
1000000 loops, best of 3: 1.44 µs per loop
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Speed of matrix multiplication

Stéfan van der Walt
On 20/04/2008, Keith Goodman <[hidden email]> wrote:
> Why is a.T*b slower than M.dot(a.T, b)? Does it take longer to parse
>  or something?

Looks like a bit of overhead.  If I change the number of elements from
500 to 500000, the difference disappears.

Regards
Stéfan
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Speed of matrix multiplication

Travis Oliphant-5
In reply to this post by Keith Goodman
Keith Goodman wrote:
> Why is a.T*b slower than M.dot(a.T, b)? Does it take longer to parse
> or something?
>  
The issue I think is that a is a Python class and so takes a bit longer
to do all the things being done which includes:

1) running the Python __mul__ function
2) creating a new array (including running the __array_finalize__ Python
function).

-Travis


_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion