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

Discussion in 'Python' started by Zhang Le, Mar 4, 2005.

  1. Zhang Le

    Zhang Le Guest

    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[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
     
    Zhang Le, Mar 4, 2005
    #1
    1. Advertising

  2. Zhang Le

    Terry Reedy Guest

    "Zhang Le" <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Mar 4, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jose flanigan
    Replies:
    0
    Views:
    410
    jose flanigan
    Jul 3, 2003
  2. Kamilche
    Replies:
    1
    Views:
    744
    Christopher T King
    Jul 14, 2004
  3. Replies:
    6
    Views:
    272
    Keith Thompson
    May 25, 2006
  4. Opening (MatLab) mat file from C

    , Oct 13, 2006, in forum: C Programming
    Replies:
    1
    Views:
    536
    Tom St Denis
    Oct 13, 2006
  5. Joza
    Replies:
    13
    Views:
    194
    -berlin.de
    Oct 3, 2006
Loading...

Share This Page