Math problem

Geoff Canyon Rev gcanyon+rev at gmail.com
Sun Feb 19 03:48:21 EST 2012


On Sun, Feb 19, 2012 at 12:58 AM, Kay C Lan <lan.kc.macmail at gmail.com>wrote:

> I've tweaked your solution only slightly as you solution only worked for
> quarter hour increment, whilst both mine and Paul's would work for any
> required increment . I simply replace your fixed 4 with the 3600/increment
> that both Paul and I were using.
>

Bother -- I saw the optimization for my own routine but forgot to apply it
to the other two.


>
> With this correction it seems Paul slips into the lead:
>
> For 1000000 cycles
> K's solution = 2378ms
> Paul's solution = 2021ms
> Geoff's solution = 2283ms
>

I'm surprised that a div isn't faster than a /, but since (on checking) a
div seems to work perfectly well with non-integers -- 4.5 div 1.5 = 3, for
example -- I have to think that within the engine it's really just a / with
the results trunc'd.


> To include a variable increment I've used an array rather than the simple
> list you used in your script, which seems to have slowed things down, as I
> got similar times to your original output, but still I'm surprised it's
> twice as slow.
>

"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. Here's my latest optimization. All three
options are similar. Here are a couple runs:

For 1000000 cycles
K's solution = 1643ms
Paul's solution = 1496ms
Geoff's solution = 1533ms

For 1000000 cycles
K's solution = 1618ms
Paul's solution = 1544ms
Geoff's solution = 1577ms

For 1000000 cycles
K's solution = 1667ms
Paul's solution = 1584ms
Geoff's solution = 1530ms

I tried longer tests, but they're still really close.



on mouseUp
   put 1000000 into tRepeats
   put 1329494400 into tStartTime
   --create an array of variable end times and increments
   repeat with i = 1 to tRepeats
      put  (1329494400 + random(36000)) into aTime[i]["End"]
      put 300 * random(6) into aTime[i]["Increment"]
   end repeat

   --K solution
   put the millisec into tStartClock
   repeat for each key i in aTime
      get aTime[i]["Increment"]
      put round(((aTime[i]["End"] + (it/2) - 1 - \
             tStartTime)/it),0) * it /3600 & cr after tStore2
   end repeat
   put the millisec - tStartClock into tTotalTime1


   --Paul's  maxless solution
   put the millisec into tStartClock
   repeat for each key i in aTime
      get aTime[i]["Increment"]
      put round(((aTime[i]["End"] -tStartTime)/it)+\
      0.4999,0) * it /3600 & cr after tStore3
   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 key i in aTime
      get aTime[i]["Increment"]
      put (aTime[i]["End"] - tStartTime + it - 1) \
             div it * it / 3600 & cr after tStore4
   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