Most Effecient way to repeat a handler/function - Found word(s) list error in the Text body

Geoff Canyon gcanyon at gmail.com
Sat Feb 15 16:37:36 CET 2014


For anyone who cares, I re-wrote Bob's code to handle a variable-length
string like so:

on mouseUp
   put 10 into wc
   repeat with i = 1 to wc
      put i & space after tData
   end repeat
   put the ticks into line one of theTime
      repeat 1000000 div wc
            repeat with j = 1 to wc
                  put word j of tData into theWord
            end repeat
      end repeat

      put the ticks into line 2 of theTime

      repeat 1000000 div wc
            repeat for each word theWord in tData
                  --get theWord
            end repeat
      end repeat

      put the ticks into line 3 of theTime
      put ("Example 1 took " & line 2 of theTime - line 1 of theTime)  & "
ticks" into line 4 of theTime
      put ("Example 2 took " & line 3 of theTime - line 2 of theTime)  & "
ticks" into line 5 of theTime
      put theTime into fld 1
end mouseUp


with that I get:

83548656597

83548656614

83548656619

Example 1 took 17 ticks

Example 2 took 5 ticks


with wc = 1000 I get:


83548666360

83548666715

83548666719

Example 1 took 355 ticks

Example 2 took 4 ticks


Note that the second example is getting faster because I'm shortening the
repeat count for longer


with wc = 100000 I had to change the outer repeat loop to execute only once
-- I got:


83548674504

83548678993

83548678994

Example 1 took 4489 ticks

Example 2 took 1 ticks


it's important to note that the first loop doesn't scale linearly with the
size of the input. The example with wc = 1000  executes the outer loop
1/100th as many times as the example with wc = 10, so to make the times
equivalent you'd multiply by 100 and get:


Example 1 took 35500 ticks

Example 2 took 400 ticks

So the second example *does* scale linearly -- 100x the size of the input =
roughly 100x the time: 400 ticks vs. 5 ticks. The first example -- 35500
vs. 17 -- that's nowhere near linear. And multiplying the size of the input
by 100 again in the third trial, if the outer loop had run as many times,
the performance would have been roughly


Example 1 took 448900000 ticks

Example 2 took 100000 ticks

Again, the second example scales roughly linearly and would finish in about
30 minutes, while the first example would take almost 3 months to finish.


On Fri, Feb 14, 2014 at 9:23 AM, Geoff Canyon <gcanyon at gmail.com> wrote:

>
> > On Feb 13, 2014, at 11:12 PM, "J. Landman Gay" <jacque at hyperactivesw.com>
> wrote:
> >
> > It's pretty well established that the "for each" form is a magnitude
> faster than the counting form.
>
> Nitpicking, but "[order of] magnitude" doesn't come close to covering the
> efficiency. The example Bob gave, since there were only ten items in the
> line, dramatically *understates* the benefits of repeat for each, which
> scales with the length of the source. A 1,000 or 100,000 item source would
> show much more than a magnitude of improvement.


More information about the use-livecode mailing list