Efficiency question for list modification

FlexibleLearning admin at FlexibleLearning.com
Fri Mar 11 06:24:04 EST 2011

Proof of how optimized syntax can make an enormous difference to speed (by
orders of magnitude in this case).

This is BAD...
  repeat for each line L in tData
    add 1 to n
    put (item 1 of L/div1) into item 1 of line n of stdout
    put (item 2 of L/div2) into item 2 of line n of stdout
  end repeat

This is GOOD...
  repeat for each line L in tData
    put (item 1 of L/div1) &","& (item 2 of L/div2) &CR after stdout
  end repeat

The Rule is: Less is More. Obvious with hindsight. Should have optimized
before asking.

Note to self: Do not expect to function on 4 hours of sleep.

Thanks to all who investigated and contributed. Wrists considered slapped.

Hugh Senior

--- Original was:

I have a long list of several thousand lines.
Each line contains two comma-separated numbers.
I want to divide the first item of each line by one divisor, and divide the
second item of each line by a different divisor.
The list order must stay the same.

Using 2 and 5 as divisors...
would become

1. Using "repeat with n=1 to num of lines" takes far too long.
2. Using "repeat for each line L" either attempts to modify read-only data,
is only 25% faster using a dumping variable.
3. Using split/combine will mess up the ordering (numeric array keys are not
sorted numerically with combine).

Any other ideas?

Hugh Senior

More information about the use-livecode mailing list