:

## Calling procedures from for loops makes calculation take longer???

Hello everyone, I have written a procedure that makes some calculations for me, which I am calling repeatedly using a simple for loop. Basically what I am doing is using the for loop to change one of the several input parameters to the procedure and then use the result. Here is a quick example: > transmission0:=Matrix(1200,2): reflection0:=Matrix(1200,2): > for m from 1 to 1200 do > freq:=0.0416666666*m: > rcwa(31,0.0,0.2998/freq,6.0e-3,1.0,2234.950223+2237.186291*I,2.042123126+0.1633593958e-1*I,2234.950223+2237.186291*I,1.0,0.8333,18.0e-6,356.0e-6,18.0e-6): > transmission0[m,1]:=freq: >transmission0[m,2]:=te[M+1]: > reflection0[m,1]:=freq: >reflection0[m,2]:=re[M+1]: > end do: My procedure "rcwa" contains all local variables, apart from vectors "te" and "re", and "M" (an integer) which are global and, as you can see, used to control the output of the for loop "transmission0" and "reflection0" which are subseqeuently plotted. My problem is that doing this seems to make the whole thing run slower than normal for some reason I do not understand. For example, when I call my procedure once by hand it takes approximately 70s to run (depending on my other parameters, and which is constant as I change the parameter I change in the for loop). However, when I use a for loop as above, the procedure still takes approximately 70s to run the first time round (e.g. m=1 here) but then gets slower and slower and eventually settles to about 250s per cycle "m". Furthermore, this effect seems more severe as I use larger matrices/vectors in my procedure (ones that are local to my procedure, not those shown above), obviously the whole thing slows down but the relative slowing as it calculates through the for loop seems worse. Also it appears the memory useage just keeps rising even though only "transmission0" and "reflection0" should be causing an increase in memory useage (a very small one at that as all they contain are simple floats) as the 3 global variables "te", "re" and "M" just get overwritten. Am I just doing something really stupid that is causing it to slow down??? Thank you very much for your time, Martyn.

﻿