octave-maintainers
[Top][All Lists]
Advanced

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

unexpected copying


From: John W. Eaton
Subject: unexpected copying
Date: Mon, 12 Jun 2006 15:21:14 -0400

On 12-Jun-2006, Paul Kienzle wrote:

| I'm getting unexpected quadratic behaviour from an assignment
| to an array after reshape in 2.9.5 (Debian):
| 
|   S=T=[100:100:800]; 
|   for i=1:length(S), 
|     k=S(i); A=rand(k); 
|     tic; A=reshape(A,k*10,k/10); A(1,1)=2; T(i) = toc; 
|   end
| 
|   [P,R] = polyfit(S,T,2);
|   plot(S,T,S,polyval(P,S))
| 
| Presumably the array is being copied.  Without the
| assigment A(1,1)=2 the time is constant, so the copy isn't
| happening in reshape.  Similarly, without the reshape the
| time is constant, so the copy isn't happening in the assignment.
| I wasn't able to demonstrate a memory leak either, so it is
| not a simple reference counting issue.
| 
| Any guesses?

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.

This is just an observation and I'm not suggesting it is the way to
fix the problem.  The result of the reshape operation shouldn't be
stored somewhere.

Note the value of count in the output from

  octave:1> A = rand; __print_symbol_info__ ("A"),  __print_symbol_info__ ("A")
  symbol_def::count: 1
    type_name: scalar
    count:     2
    rep info:  no info for type: scalar
  symbol_def::count: 1
    type_name: scalar
    count:     1
    rep info:  no info for type: scalar

Here, count is the reference count for the octave_value object that
"A" refers to.  Also note the difference with this:

  A = rand; ans = []; __print_symbol_info__ ("A")
  symbol_def::count: 1
    type_name: scalar
    count:     1
    rep info:  no info for type: scalar

Hmm.

jwe


reply via email to

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