Efficient code for accessing the items (or lines) in a variable.

Alex Tweedly alex at tweedly.net
Sun Feb 20 19:11:11 CST 2011


Hmmm, no you can't use
>    if tItem is empty then
>       exit repeat
>    end if
because it is perfectly feasible to have empty items within the middle 
of the data. You would need to use

> if tOffset > the number of chars in tData then exit repeat
or something like that. I'm not sure whether this would be more 
efficient then "repeat with ..." or not - it depends on the internal 
representation, and whether "the number of chars in variable" requires a 
scan through the variable or not.


Using "the number of chars in tItem" rather than "the number of chars in 
item 1 of (char tOffset to -1)" is a good idea, though I doubt if the 
performance gain would be significant. In some cases (such as the 
original code that triggered this thread, where you only process every 
second item), putting the item into t temp variable could even cost some 
performance.

But the real reason I didn't do it that way was that I was talking about 
the possibility of changing the item in question within the loop, and 
that means you need to use "the number of chars in the item within the 
variable" at the end of the loop.

With those changes, the timings do work out about 20% faster using the 
local variable tItem rather than extracting the substring a second time 
- so worth doing that if you need to have the data in a temp variable, 
and if you do not change the item within the variable.

-- Alex.

On 20/02/2011 09:07, Dick Kriesel wrote:
> On 2/19/11 4:44 PM, "Alex Tweedly"<alex at tweedly.net>  wrote:
>
>>> put 1 into tCurrentItemCharOffset
>>>    repeat with i = 1 to the number of items in tData
>>>      ... item 1 of (char tCurrentItemCharOffset to -1 of tData) ...
>>>      add the number of chars in item 1 of (char tCurrentItemCharOffset
>>> to -1 of tData) + 1 to tCurrentItemCharOffset
>>>    end repeat
> Hi, Alex. Since technique 3 involves counting the items and then for each
> item twice extracting a substring, perhaps a technique 4 is worth comparing:
>
> put 1 into tOffset
> repeat forever -- no counting the items
>    put item 1 of (char tOffset to -1 of tData) into tItem -- single
> extraction per item
>    if tItem is empty then
>       exit repeat
>    end if
>    add (the number of chars in tItem) + 1 to tOffset
>    -- do something useful with tItem
> end repeat
>
> Is it any faster on your data?
>
> -- Dick
>
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>




More information about the use-livecode mailing list