Math problem
Peter M. Brigham, MD
pmbrig at gmail.com
Fri Feb 17 23:26:29 EST 2012
Well, there's something wrong with the K solution and the Paul solution. Stepping through the script with the debugger, for the first line of tStore, the difference between tEndTime and tStartTime = 7974 (I inserted a temp variable tTemp = tEndTime - tStartTime so I could see what was going on). You want to round upwards to the nearest multiple of 900, which should be 8100. Both the K and Paul solutions give a value of 2.5 -- clearly a really wrong answer. If you amend the Geoff solution as follows:
--Geoff's itemoffset solution
put the millisec into tStartClock
repeat for each line tLine in tStore
put item 1 of tLine into tStartTime
put item 2 of tLine into tEndTime
put item 3 of tLine into tIncrement
put (x div tIncrement)*tIncrement + item itemOffset((x mod \
tIncrement > 0),"true,false") of (tIncrement,0) \
& cr after tStore4
end repeat
you get 8100, which is correct, and eyeballing the first few lines indicates that Geoff's solution seems to be accurate.
I fixed the Geoff solution because I understand it. I'm too tired to figure out the errors in the implementation of the other two, sorry.
-- Peter
Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig
On Feb 17, 2012, at 8:55 PM, Kay C Lan wrote:
> Guys I thought I'd speed test these.
>
> Unfortunately I don't have the time to figure out exactly how Geoff's
> amended version of Peter's solution actually works in this case; rounding
> 1.1 to 1.25, 3.3 to 3.5. I can get it to work with whole numbers, but not
> 0.25 increments as per the OP.
>
> The current script produces:
>
> For 10000 cycles
> K's solution = 25ms
> Paul's solution = 28ms
> Geoff's solution for = 39ms
> Geoff's solution doesn't = K's
> Geoff's solution doesn't = Paul's
>
> Please amend the script below:
>
> on mouseUp
> put 10000 into tRepeats
> --to create a list of times to use.
> --900 is used as fixed 1/4 hour interval
> repeat tRepeats times
> put 1329494400 & "," & \
> (1329494400 + random(36000)) & "," & \
> 900 & cr after tStore
> end repeat
>
> --K solution
> put the millisec into tStartClock
> repeat for each line tLine in tStore
> put item 1 of tLine into tStartTime
> put item 2 of tLine into tEndTime
> put item 3 of tLine into tIncrement
> put round(((tEndTime + (tIncrement/2) - 1 -
> tStartTime)/tIncrement),0)/(3600/tIncrement) & cr after tStore2
> end repeat
> put the millisec into tEndClock
> put tEndClock - tStartClock into tTotalTime1
>
> --Paul's max solution
> put the millisec into tStartClock
> repeat for each line tLine in tStore
> put item 1 of tLine into tStartTime
> put item 2 of tLine into tEndTime
> put item 3 of tLine into tIncrement
> put
> round(((tEndTime-tStartTime)/max(tIncrement,60))+0.4999,0)/(3600/max(tIncrement,60))
> & cr after tStore3
> end repeat
> put the millisec into tEndClock
> put tEndClock - tStartClock into tTotalTime2
> if (tStore2 <> tStore3) then
> put "Paul's solution doesn't = K's" & cr after tErrors
> end if
>
> --Geoff's itemoffset solution
> put the millisec into tStartClock
> repeat for each line tLine in tStore
> put item 1 of tLine into tStartTime
> put item 2 of tLine into tEndTime
> put item 3 of tLine into tIncrement
> put (tEndTime - tStartTime)/tIncrement into x
> put 4 into i --tried 900, 4, 25 and 0.25
> put (x div i*i) + (item itemOffset((x mod i > 0),"true,false") of
> (i,0)) & cr after tStore4
> end repeat
> put the millisec into tEndClock
> put tEndClock - 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 msg
> put "K's solution = " & tTotalTime1 & "ms" & cr after msg
> put "Paul's solution = " & tTotalTime2 & "ms" & cr after msg
> put "Geoff's solution for = " & tTotalTime3 & "ms" & cr after msg
> put tErrors after msg
> end mouseUp
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list