Math problem

Kay C Lan lan.kc.macmail at gmail.com
Sun Feb 19 04:32:01 EST 2012


On Sun, Feb 19, 2012 at 4:48 PM, Geoff Canyon Rev <gcanyon+rev at gmail.com>wrote:

> "repeat for each line" is incredibly fast, so I'm not surprised that it
> beats an array. Interestingly, I was able to speed up all three solutions
> by doing this:
>
>      get aTime[i]["Increment"]
>
> and then using "it" in the math makes things faster. Arrays aren't just
> slow, they're slow every time.
>

That is very interesting.

I appreciate the general rule is 'repeat for each..." is the fastest
repeat, but I didn't appreciate the difference between item/line and key.

Here's my latest amendment, which is getting a bit silly. It no longer
amends any of the offered solutions, just the method of feeding them
numbers. No more arrays, just nested repeat for each loops.

For 1000000 cycles
K's solution = 12730ms
Paul's solution = 11707ms
Geoff's solution = 12064ms

For 1000000 cycles
K's solution = 12767ms
Paul's solution = 11755ms
Geoff's solution = 12136ms

For 1000000 cycles
K's solution = 12759ms
Paul's solution = 11709ms
Geoff's solution = 12073ms

on mouseUp
   put 1000000 into tRepeats
   put 1329494400 into tStartTime
   --create a list of end times
   repeat with i = 1 to tRepeats
      put  (1329494400 + random(36000)) & cr after tStore
   end repeat
   put "300,600,900,1200,1500,1800" into tAllIncrements

   --K solution
   put the millisec into tStartClock
   repeat for each line tEndTime in tStore
      repeat for each item tIncrement in tAllIncrements
         put round(((tEndTime + (tIncrement/2) - 1 - \
               tStartTime)/tIncrement),0) * tIncrement /3600 & cr after
tStore2
      end repeat
   end repeat
   put the millisec - tStartClock into tTotalTime1


   --Paul's  maxless solution
   put the millisec into tStartClock
   repeat for each line tEndTime in tStore
      repeat for each item tIncrement in tAllIncrements
         put round(((tEndTime - tStartTime)/tIncrement)+\
         0.4999,0) * tIncrement /3600 & cr after tStore3
      end repeat
   end repeat
   put the millisec - tStartClock into tTotalTime2
   if (tStore2 <> tStore3) then
      put "Paul's solution doesn't = K's" & cr after tErrors
   end if


   --Geoff's revised any increment solution
   put the millisec into tStartClock
   repeat for each line tEndTime in tStore
      repeat for each item tIncrement in tAllIncrements
         put (tEndTime - tStartTime + tIncrement - 1) \
               div tIncrement * tIncrement / 3600 & cr after tStore4
      end repeat
   end repeat
   put the millisec - tStartClock into tTotalTime3
   if (tStore2 <> tStore4) then
      put "Geoff's solution doesn't = K's" & cr after tErrors
   end if
   if (tStore3 <> tStore4) then
      put "Geoff's solution doesn't = Paul's" & cr after tErrors
   end if

   put "For " & tRepeats & " cycles" & cr into R
   put "K's solution = " & tTotalTime1 & "ms" & cr after R
   put "Paul's solution = " & tTotalTime2 & "ms" & cr after R
   put "Geoff's solution = " & tTotalTime3 & "ms" & cr after R
   put R & tErrors
end mouseUp



More information about the use-livecode mailing list