Speed optimisation

Dick Kriesel dick.kriesel at mail.com
Wed Oct 5 05:15:01 EDT 2005


On 10/4/05 11:58 PM, "Rob Beynon" <r.beynon at liv.ac.uk> wrote:

> Dear Colleagues,
> 
> I have a function that takes a time in this format
> 
> hh:mm:ss.s
> 
> and to which I add a variable number of seconds, then output the
> updated time in the same format. hh can be greater than 24!
> 
> Here's the function. Problem is, it seems slow (I need to do this call
> about 150,000 times each file I process). I would appreciate any
> insights into making this function faster
> 
> function newTime oldTime,addedSec
>     put the replacetext(oldTime,":"," ") into splitTime
>     put the first word of splitTime into h
>     put the second word of splitTime into m
>     put the third word of splitTime into s
>     put s + addedSec into newSec
>     put newSec mod 60 into remainSec
>     put (newsec-remainSec)/60 into addedMin
>     put m + addedMin into newMin
>     put newMin mod 60 into remainMin
>     put (newMin-remainMin)/60 into addedHr
>     put h + addedHr into newHr
>     if length(remainSec) = 1 then put "0" & remainSec into remainSec
>     if length(remainMin) = 1 then put "0" & remainMin into remainMin
>     if length(newHr) = 1 then put "0" & newHr into newHr
>     put newHr & ":" & remainMin & ":" & remainSec into newTime
>     return newTime
> end newTime

On my machine, executing your function 150,000 times took 15 seconds.
Unless you have a lot of files or you're really pressed for time, your
function seems fast enough to me.

But if you feel the need for speed, here's a similar function, that worked
in my few tests, and that took 5.7 seconds for 150,000 executions:

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 & ":" & tSeconds mod 3600 div 60 & ":" \
    & 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

I'd expect there'd be an even faster way.  (Sarah?)

-- Dick





More information about the use-livecode mailing list