Math problem

Geoff Canyon Rev gcanyon+rev at gmail.com
Sat Feb 18 23:22:16 EST 2012


On Fri, Feb 17, 2012 at 7:55 PM, Kay C Lan <lan.kc.macmail at gmail.com> wrote:

> Guys I thought I'd speed test these.


Given that we're dealing in seconds (and therefore integers), the very
clever itemoffset idea Peter came up with is unnecessary. Here's a
comparison of the three options, with mine tweaked to be similar to my
original idea (and to return hours, so it matches the output of the other
two).

I took the liberty of removing the "max" function from Paul's code, since
all it does is restrict the rounding interval to a one minute interval,
which was not part of the original spec, and also since this test uses a
constant 15 minute interval, completely unused here. I got this on my
MacBook:

For 1000000 cycles
K's solution = 1342ms
Paul's solution = 1226ms
Geoff's solution = 1095ms

Which is to say that, at about 1 million calculations per second, any of
these solutions would be fine in practice.

gc

on mouseUp
  put 1000000 into tRepeats
  --to create a list of times to use.
  --900 is used as fixed 1/4 hour interval
  put 1329494400 into tStartTime
  put 900 into tIncrement
  repeat with i = 1 to tRepeats
     put  (1329494400 + random(36000)) & cr after tStore
  end repeat

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

  --Paul's  max solution
  put the millisec into tStartClock
  repeat for each line tEndTime in tStore
     put round(((tEndTime-tStartTime)/tIncrement)+\
     0.4999,0)/(3600/tIncrement) & 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 mod solution
  put the millisec into tStartClock
  repeat for each line tEndTime in tStore
     put (tEndTime - tStartTime + tIncrement - 1) \
     div tIncrement / 4 & 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