Page 1 of 1

Efficiency in repeat loops

Posted: Sat Nov 14, 2009 6:35 pm
by sturgis
Hey all, I have the code listed below and have a couple questions regarding its efficiency.

It appears that the efficiency of the code degrades in direct proportion to the number of loops. Meaning 100 loops when the script is run takes
Processing time: 107.00 Milliseconds. Each dataline took: 1.07 milliseconds
200 loops takes
Processing time: 421.00 Milliseconds. Each dataline took: 2.10 milliseconds
and the degradation gets worse from there.

I think I read elsewhere that repeat with i = 1 to number loops just aren't all that efficient, but haven't located the post yet.

Should I just avoid using repeat with i = style loops and keep the count myself if the number of loops is large, and could someone explain the reason things behave this way? It just seems logical that each loop would take the same amount of time no matter how many loops there are since the same thing is done for all but the very first loop, so curiosity is beating me up.

Code: Select all

global theDataA
on mouseUp
   lock screen
   set the numberFormat to "#.00"
   put the numberFormat
   put true into firstLoop
   put empty into theDataA
   put empty into field outField
   put field "periods" into tCount
   
   put "Period" & tab & "Accumulated Deposits" & tab & "Interest" & tab & "Accumulated Interest" & tab & "New Balance" & return into field "outField"
   put the milliseconds into theTimerStart
   repeat with i = 1 to the value of field "periods"
      if firstLoop then
         put i into thedataA[i]["Period"]
         put field startbal * (field rate / 12) into theDataA[i]["Interest"]
         put field startbal + theDataA[i]["Interest"]  into theDataA[i]["New Balance"]
         put field startbal into theDataA[i]["Accumulated Deposits"]
         put theDataA[i]["Interest"] into thedataA[i]["Accumulated Interest"]
         put false into firstLoop
      else
         put i into thedataA[i]["Period"]
         put (theDataA[i-1]["New Balance"] + field "monthlyAdd" )* (field rate / 12) into theDataA[i]["Interest"]
         put theDataA[i-1]["New Balance"] + theDataA[i]["Interest"] + field "monthlyAdd"  into theDataA[i]["New Balance"]
         put field "monthlyAdd" + theDataA[i-1]["Accumulated Deposits"] into theDataA[i]["Accumulated Deposits"]
         put theDataA[i]["Interest"] + theDataA[i-1]["Accumulated Interest"] into thedataA[i]["Accumulated Interest"]
      end if
      repeat for each item thisItem in "Period,Accumulated Deposits,Interest,Accumulated Interest,New Balance"
         put thedataA[i][thisItem] & tab after field "outField"
      end repeat
      put return after field "outField"
   end repeat
   
   put theDataA[tCount]["Accumulated Deposits"] into field "Total Deposits"
   put theDataA[tCount]["Accumulated Interest"] into field "Total Interest"
   put theDataA[tCount]["New Balance"] into field "End Balance"
   put the milliseconds - theTimerStart into theProcessingTime
   put "tCount is: " && tCount && "Processing time:" && theProcessingTime && "Milliseconds. Each dataline took: " && theProcessingTime / field "periods" && "milliseconds"
   unlock screen
end mouseUp

Posted: Sat Nov 14, 2009 7:15 pm
by whelkybaby
Hi Sturgis,

I wonder whether these suggestions might speed this up a little?

I notice that you're fetching the contents of a field called "monthlyAdd" in each loop, which will take a little bit of time. You could try putting this into a variable.

Modifying the contents of the field called "outField" in each loop is really going to have an impact on performance. Instead, put whatever you were going to put into the field into a variable and then put the lot into "outField" in one go.

What's happening is that when you use 'after', in order for Rev to find the last item in the field, it has to start from the top and work its way down the field. As more items go into the field, it takes longer to find the last one.

Let's see how this performs now?

Cheers,


Steve
www.theworcestersource.com

Posted: Sat Nov 14, 2009 7:22 pm
by sturgis
OOOOHHH! You know, I think I knew that! Ty ty very very much, I was having a major brain block. TY!
whelkybaby wrote:Hi Sturgis,

I wonder whether these suggestions might speed this up a little?

I notice that you're fetching the contents of a field called "monthlyAdd" in each loop, which will take a little bit of time. You could try putting this into a variable.

Modifying the contents of the field called "outField" in each loop is really going to have an impact on performance. Instead, put whatever you were going to put into the field into a variable and then put the lot into "outField" in one go.

What's happening is that when you use 'after', in order for Rev to find the last item in the field, it has to start from the top and work its way down the field. As more items go into the field, it takes longer to find the last one.

Let's see how this performs now?

Cheers,


Steve
www.theworcestersource.com

Posted: Sat Nov 14, 2009 8:00 pm
by whelkybaby
Yay!

*bows*