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
200 loops takesProcessing time: 107.00 Milliseconds. Each dataline took: 1.07 milliseconds
and the degradation gets worse from there.Processing time: 421.00 Milliseconds. Each dataline took: 2.10 milliseconds
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