Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

[Numeric] column vector faster than row vector in mat multiply?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


sigu4wa02 at sneakemail

Mar 4, 2005, 8:14 AM

Post #1 of 2 (536 views)
Permalink
[Numeric] column vector faster than row vector in mat multiply?

Hi,
I did a small benchmark of matrix-vector multiply operation using
Numeric module. I'm a bit suprised to find matrix*col-vector is much
faster than row-vector*matrix. I wonder whether other people have
observed this fact too, and why?

Below is the code I used, with output from my machine.

python bench.py
running 1000 iterations of matrix multiply of row 1000-vector
10.5609340668 sec
running 1000 iterations of matrix operation of column 1000-vector
4.11953210831 sec

-----code begin-----
import random
import time
from Numeric import *

n = 1000
k = 1000

r = array([ random.gauss(0, 1) for i in range(n)])
c = array([ [random.gauss(0, 1)] for i in range(n)])

M = zeros((n, n), Float)
for i in range(n):
for j in range(n):
M[i][j] = random.gauss(0, 1)

print 'running %d iterations of matrix multiply of row %d-vector' % (k,
n)
t = time.time()
for i in xrange(k):
matrixmultiply(r, M)
print time.time()-t, 'sec'

print 'running %d iterations of matrix operation of column %d-vector' %
(k, n)
t = time.time()
for i in xrange(k):
matrixmultiply(M, c)
print time.time()-t, 'sec'

------code end----------


Zhang Le

--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Mar 4, 2005, 12:27 PM

Post #2 of 2 (507 views)
Permalink
Re: [Numeric] column vector faster than row vector in mat multiply? [In reply to]

"Zhang Le" <sigu4wa02 [at] sneakemail> wrote in message
news:1109952846.225378.136210 [at] o13g2000cwo
> Hi,
> I did a small benchmark of matrix-vector multiply operation using
> Numeric module. I'm a bit suprised to find matrix*col-vector is much
> faster than row-vector*matrix. I wonder whether other people have
> observed this fact too,

Yes, common knowledge in numerical analysis community. Using the faster
direction for a particular system as much as possible is part of tuning
linear algebra software.

> and why?

I presume that Numeric, like Python, stores matrices by row. So M*v
multiplies contiguous rows by a contiguous vector. Multiplying a vector by
non-contiguous columns requires requires skipping thru the matrix, which
may require more computation and generate more cache misses and page
faults.

Terry J. Reedy



--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.