Loop slow? (was: Up and running!)

Geoff Canyon gcanyon at inspiredlogic.com
Sat Dec 29 13:13:10 EST 2001


At 1:10 PM +0100 12/29/01, Andre Rombauts wrote:
>Of course this is quicker: the loop is performed 'outside' the screen
>display process. But you can hear clearly the 2 beep: the loop takes some
>time to run...
>
>on mouseUp
>  set the text of field "info" to empty
>  beep
>  repeat with k = 32 to 129
>    if k = 65 or k= 97 then put linefeed & linefeed after varS
>    put numTochar(k) & " " after varS
>  end repeat
>  beep
>  set the text of field "info" to varS
>end mouseUp

The beeps are not a good indicator of how long something takes. Here's a better way to test (note the use of the ticks() function):

on mouseUp
  set the text of field "info" to empty
  put ticks() into tStart
  repeat with k = 32 to 129
    if k = 65 or k= 97 then put linefeed & linefeed after varS
    put numTochar(k) & " " after varS
  end repeat
  put varS into field "info"
  put ticks() - tStart
end mouseUp

Note that it only updates the field at the end, but that the field update _is_ included in the timing. This takes from 0 to 1 ticks (1/60th of a second) on my computer. The ticks are displayed after it runs in the message box.

The script below takes about 25 ticks -- note that I'm not including updating the field, but I am doing the variable creation 1000 times:

on mouseUp
  set the text of field "info" to empty
  put ticks() into tStart
  repeat 1000 times
    put empty into varS
    repeat with k = 32 to 129
      if k = 65 or k= 97 then put linefeed & linefeed after varS
      put numTochar(k) & " " after varS
    end repeat
  end repeat
  put ticks() - tStart
  put varS into field "info"
end mouseUp

Finally, if you really need the speed (not that there aren't faster ways that subvert the nature of the test) you can unroll the repeat structure from the if statements. The following performs the same work, but gets it done (1000 times) in 15 ticks:

on mouseUp
  set the text of field "info" to empty
  put empty
  put ticks() into tStart
  repeat 1000 times
    put empty into tVarS
    repeat with k = 32 to 64
      put numToChar(k) & " " after tVarS
    end repeat
    put cr & cr after tVarS
    repeat with k = 66 to 98
      put numToChar(k) & " " after tVarS
    end repeat
    put cr & cr after tVarS
    repeat with k = 99 to 129
      put numToChar(k) & " " after tVarS
    end repeat
  end repeat
  put ticks() - tStart
  put tVarS into field "info"
end mouseUp

regards,

Geoff



More information about the use-livecode mailing list