|
From: | Doug Stewart |
Subject: | Re: Initializing variables |
Date: | Fri, 20 Jun 2008 21:02:18 -0400 |
User-agent: | Thunderbird 2.0.0.14 (X11/20080505) |
asha g wrote:
I have a program with several variables - all are vectors or matrices. Now do I initialize the ones I need to save and plot at the beginning of the program or all the variables that are used in the program? eg - if I need vvvv and abhh do I just write: vvvv= zeros(niter, N); abhh = zeros(niter,N); before the start of the script file or shd I do that to each one of them. When niter is large and N is large, this takes up lots of space and slows down operations. However, when I don't do it to all the variables I am not sure what is going on especially as there are many loops inside. I hope this is clear. ThanksAsha_______________________________________________Help-octave mailing list address@hidden https://www.cae.wisc.edu/mailman/listinfo/help-octave
One thing you should understand.If you use a new array with small indexes as a(2,3)=6, and then keep using bigger and bigger indexes, then Octave sets aside a small space in unused memory at the start and then when you use larger indexes then Octave must go and set aside a larger space in the heap space and copy all the first values the the new area. It must do this each time you make the array bigger. But it you pre define the array to the maximum size that it will need. then it goes to the heap and sets aside all the needed memory in one operation and is much faster.
All your array don't need to be defined at the start, just make sure you set up then full size of the array just before you start using it.
I am not sure that this answers your question but it sure helps to speed up my students' programs.
HTH Doug Stewart
[Prev in Thread] | Current Thread | [Next in Thread] |