Speed optimisation

Dick Kriesel dick.kriesel at mail.com
Thu Oct 6 23:56:05 EDT 2005


On 10/6/05 4:21 AM, "Alex Tweedly" <alex at tweedly.net> wrote:

> Hmmm - can you give a case where Kay's method gives wrong answers
> because of a negative seconds value ?
> As far as I can see (both by inspection and by testing) it always gets
> it right.
 
Whenever the negative seconds cause the result to be negative, the result is
wrong.  For example, "00:01:00.5",-61 gives 23:59:59.5.  But even if the
convert function didn't cause a wrap-around, the result would be wrong,
because the decimal digit is appended without checking the sign. The
function would compute (60-61)&.5 = -1.5 seconds, instead of -.5 seconds.

> On the other hand, both your method and mine (which is really just a
> variant of yours) get it wrong in key cases of negative seconds (e.g.
> 00:00:00.1   - 31 gives   0:00:0-31 for you and 0:00:0-31.0 for me -
> both rather hopeless :-)

So I'm glad I wasn't throwing any stones.  Here's a new version that handles
negative seconds and negative results correctly (I think):

function addTimeAndSeconds pTime,pSeconds
  set the itemDelimiter to ":"
  put item 1 of pTime * 3600 + item 2 of pTime * 60 + item 3 of pTime \
    + pSeconds into tSeconds
  put tSeconds div 3600 & ":" & abs(tSeconds) mod 3600 div 60 & ":" \
    & abs(tSeconds) mod 60 into tTime
  if item 2 of tTime < 10 then put 0 before item 2 of tTime
  if item 3 of tTime < 10 then put 0 before item 3 of tTime
  return tTime
end addTimeAndSeconds

The new version runs faster than my old, invalid version, oddly enough.

I wonder if Rob still cares...

-- Dick





More information about the use-livecode mailing list