[Numpy-discussion] Speed of matrix multiplication

Keith Goodman kwgoodman@gmail....
Sun Apr 20 14:03:39 CDT 2008


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


More information about the Numpy-discussion mailing list