help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: algorithm efficiency (memory allocation issue?)


From: Ted Harding
Subject: RE: algorithm efficiency (memory allocation issue?)
Date: Wed, 27 Jun 2001 10:55:15 +0100 (BST)

On 27-Jun-01 Aaron Brick wrote:
> f is a vector (of rows) and n is the number of times to reproduce it.
> try running the command "multiply ( (1:200)', 100 )" for a dramatic
> demonstration of the slowdown.

Indeed! You appear to be making a column vector which consists of
n copies of the column vector f stacked on top of each other.
You can do this quickly, with no significant slowdown, with

  function wave = multiply ( f, n )
    wave = [];
    for i=1:n, wave=[wave;f]; fprintf(stderr, "%d ",i); endfor
  endfunction

(I've left in the fprintf(stderr, "%d ",i); as a trace print).

NOW try

  multiply ( (1:200)', 100 );

There will still be a slight effect of repeated memory allocation,
and you can do away with this altogether by allocating the needed
memory at the start, though the program is a bit more complicated:

  function wave = multiply ( f, n )
    r = max(size(f));
    wave = zeros(n*r,1);
    u = 1; v = r;
    for i=1:n
      wave(u:v)=f; u = u+r; v = v+r; fprintf(stderr, "%d ",i);
    endfor
  endfunction

Cheers,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <address@hidden>
Fax-to-email: +44 (0)870 167 1972
Date: 27-Jun-01                                       Time: 10:55:15
------------------------------ XFMail ------------------------------



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]