Re: Strings & Loops
Re: Strings & Loops
- Subject: Re: Strings & Loops
- From: Nigel Garvey <email@hidden>
- Date: Thu, 02 Feb 2012 01:13:27 +0000
KOENIG Yvan wrote on Wed, 01 Feb 2012 11:43:22 +0100:
>Le 1 févr. 2012 à 11:11, Thomas Fischer a écrit :
[snip]
>> Case 3:
>> set outText to {}
>> set outTextRef to a reference to outText
>> set myTimer to start timer
>> set theChars to every character in inText
>> set theCharsRef to a reference to theChars
>> # repeat with aChar in text of inText # <- Not only much slower
(~40x),
>>but results often(?) incomplete!
>> repeat with aChar in theChars # Ref # <- Here Ref is actually slower
>>(~2x)!
>> copy aChar to the end of outTextRef # <- without Ref much slower
(~15x)
>> end repeat
>> set outText to contents of outText as text
>> set myTime to stop timer myTimer
>> log outText
>> myTime
>>
>> Ergebnis:
>> 47.761962890625
>
>This behavior was described for the first time several years ago by
Serge
>Belleudy-d’Espinose.
>
>It's described in Matt Neuburg's AppleScript The Definite Guide.
The speeding-up of list access through the use of a variable set to 'a
reference to' the list variable was shown in the 1997 AppleScript
Language Guide and was probably known about long before that.
What Serge noticed sometime around 2000/2001 was that access to list
items was also greatly speeded up when the list was in a script object.
He couldn't explain this and asked about it here. (Unfortunately I can't
find the relevant post in the archives.) It was I who (eventually) made
the connection with 'a reference to' and also realised that placing 'my'
or 'its' before a list variable had the same effect.
The speed-up happens when references to items or properties of a list
have this form:
<element(s) or property> of <list variable> of <script>
>> set theChars to every character in inText
>> set theCharsRef to a reference to theChars
Here, the value of theCharsRef is 'theChars of «script»', so 'item i of
theCharsRef' is the same as 'item i of theChars of «script»'. «script»
is of course the script in which theChars is a property, global, or
run-handler variable, so you could write instead 'item i of my
theChars'. This works very slightly faster even than 'item i of
theCharsRef' because the full reference is compiled directly into the
script instead of being partly held in a variable.
Serge's script object discovery allows the same kind of reference:
script o
property theChars: every character in theText
end
item i of theChars of o -- or 'item i of o's theChars'
It's useful in handlers, where you have local variables (which can't be
referenced) and don't want to have to resort to globals or global
properties.
>> # repeat with aChar in text of inText # <- Not only much slower
(~40x),
This is slow because each value of aChar is a reference to an individual
character in a text and each has a superfluous middle stage. The
resolution of these references later will involve the derivation of the
characters from the text one at a time.
set inText to "Hello"
set outText to {}
repeat with aChar in text of inText
copy aChar to end of outText
end repeat
outText
--> {item 1 of every text of "Hello", item 2 of every text of "Hello", item 3 of every text of "Hello", item 4 of every text of "Hello", item 5 of every text of "Hello"}
'set' is slightly faster than 'copy' too. ;)
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden