Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [Numeric] column vector faster than row vector in mat multiply?

Reply
Thread Tools

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

 
 
Zhang Le
Guest
Posts: n/a
 
      03-04-2005
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

 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      03-04-2005

"Zhang Le" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To mat or not to mat? Paul Ciszek Digital Photography 8 09-08-2012 04:00 PM
Why is a vector find faster than a set with find()? boltar2003@boltar.world C++ 21 08-13-2012 08:03 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM
Row / Column gets much bigger when editing a row in grid Bazza Formez ASP .Net 4 12-09-2004 11:03 PM



Advertisments