help-octave
[Top][All Lists]
Advanced

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

Re: Creating "sequential variable names"


From: Jaroslav Hajek
Subject: Re: Creating "sequential variable names"
Date: Tue, 19 Oct 2010 15:10:02 +0200

On Tue, Oct 19, 2010 at 9:36 AM, John W. Eaton <address@hidden> wrote:
> On 19-Oct-2010, AlbFrigerio wrote:
>
> | Thanks a lot jwe, it looks very simple, as all the right things should be. I
> | implemented it and everything works without using eval or something like
> | that.
> |
> | > I think it is even clearer if you write
> | >
> | >   for i = 1:10
> | >     AG(i).P.n = 1;
> | >     AG(i).M.err = 0;
> | >   endfor
> | >
> | > to create a struct array.
>
> I should also mention that if you have a large number of elements in
> your struct array, the above will be somewhat slow because AG will be
> resized each time through the loop.

There is also the stack-array optimization designed to combat similar
problems, e.g.
for i = 1:many
  AG(i).P = something(i);
endfor

should *not* reallocate at each step, but once each 1000 steps or so.
In the original example, however, I believe you kill the optimization
by the nested field reference, because the interpreter first has to
perform a an auto_add subsref AG(i).P to check whether AG(i).P might
be a cs-list reference. And unfortunately AFAIK the stack-array
optimization doesn't work with auto_add subsrefs because they make a
copy prior to resizing. It would be nice to overcome this. Maybe once
I have time for Octave again :)



reply via email to

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