octave-maintainers
[Top][All Lists]
Advanced

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

Re: unexpected copying


From: Paul Kienzle
Subject: Re: unexpected copying
Date: Tue, 13 Jun 2006 12:06:57 -0400
User-agent: Mutt/1.2.5.1i

On Tue, Jun 13, 2006 at 12:57:40AM -0400, John W. Eaton wrote:
> On 12-Jun-2006, John W. Eaton wrote:
> 
> | On 12-Jun-2006, John W. Eaton wrote:
> | 
> | | I think it is assignment to "ans" that is causing trouble (though
> | | "ans" is not even showing up with a value in the symbol table).  Hmm.
> | | Looks like a bug, but I don't see where yet.
> | | 
> | | Note that with your example, adding "ans = []" after reshape prevents
> | | the copy.  With this code
> | | 
> | |   S=T=[100:100:3000]; 
> | |   for i=1:length(S), 
> | |     k=S(i); A=rand(k); 
> | |     tic; A=reshape(A,k*10,k/10); ans = []; A(1,1)=2; T(i) = toc; 
> | |   end
> | | 
> | |   [P,R] = polyfit(S,T,2);
> | |   plot(S,T,S,polyval(P,S))
> | | 
> | | I see constant time.
> | 
> | Hmm, well I don't think it is ans as I also see constant time if I
> | replace "ans" with "x" in the above code.  OK, now I'm confused.
> 
> OK, please try the following patch.  It seems obvious now, but this
> one had me stumped for quite a while.  This bug has probably been
> around for a long time.  I see a few more useless assignments that can
> be deleted, and maybe this function doesn't need to return anything
> at all.  I'll check that out later.  For now, this patch should fix
> the problem you were seeing.

Thanks! Your workaround works in 2.1.72 as well:

    A = reshape(A,newshape);
    1; # Drop extra reference to A (Octave 2.9.6 and below)
    A(1,1) = 2;

Even if it is futile trying to use octave for datasets approaching the 
amount of available memory (there are no inplace operations), I suppose 
the performance will improve every time we avoid unnecessary copies.

Here's another one which I suspect will be harder to avoid:

   function t = set1(A), tic; A(1,1)=2; t=toc; end
   S=T=[100:100:3000];
   for i=1:length(S); T(i) = set1(rand(S(i))); end
   plot(S,T);

I'm guessing the argument list is holding onto a reference
to the temporary rand(S(i)) until the function call returns.

It's probably not worth your time addressing this since
it won't help the common case:

   function t = set1(A), tic; A(1,1)=2; t=toc; end
   S=T=[100:100:3000];
   for i=1:length(S); A=rand(S(i)); T(i) = set1(A); end
   plot(S,T);



        - Paul


reply via email to

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