Re: Strings & Loops
Re: Strings & Loops
- Subject: Re: Strings & Loops
- From: KOENIG Yvan <email@hidden>
- Date: Wed, 01 Feb 2012 11:43:22 +0100
Le 1 févr. 2012 à 11:11, Thomas Fischer a écrit :
> Hello,
>
> from my testing of the "Reformatting a string" thread I found some confusing results with strings, list and loops.
> If this is elaborated a some place, please let me know.
> I should note in advance that all of this is irrelevant if your script is dealing with single words or other small strings.
> But if you want to handle larger chunks of text (> 1 KB?) or want to set up a fairly universal script, these considerations might be useful.
>
> For testing I use a simple script and Tetsuro Kurita's LapTime.osax
>
> set inText to "put 5 KB of text (without quotes) here!"
> set outText to {}
> # set outText to ""
> set outTextRef to a reference to outText
> set myTimer to start timer
> set theChars to every character in inText # faster with longer texts
> set theCharsRef to a reference to theChars # much faster with longer texts
> repeat with aChar in theChars # Ref # text of inText
> # set outText to outText & aChar
> # set aChar to aChar as text
> copy aChar to the end of outTextRef # a little faster with longer texts
> end repeat
> set outText to contents of outText as text
> # set outText to outText as text
> set myTime to stop timer myTimer
> log outText
> myTime
>
> There are several decision in this script which influence the time needed (considerably!):
>
> 1. String versus list collation:
> set outText to ""
> set outText to outText & aChar
> instead of
> set outText to {}
> copy aChar to the end of outText
>
> 2. Lists versus list references:
> using
> outTextRef
> instead of
> outText
> and/or
> theCharsRef
> instead of
> theChars
>
> 3. Conversion of references to text:
> Using
> copy aChar to the end of outTextRef
> set outText to contents of outText as text
> or
> set aChar to aChar as text
> copy aChar to the end of outTextRef
> set outText to outText as text
>
> Unfortunately, the results of these decisions are not independent of each other. I cannot untangle it now but can give a few examples.
> For those who care, here are some results:
>
> Case 1:
> set outText to {}
> set myTimer to start timer
> set theChars to every character in inText
> set theCharsRef to a reference to theChars
> repeat with aChar in theChars # Ref # <- Here Ref is actually a little slower!
> copy aChar to the end of outText
> end repeat
> set outText to contents of outText as text
> set myTime to stop timer myTimer
> log outText
> myTime
> Ergebnis:
> 866.31298828125
>
> Case 2:
> set outText to ""
> set myTimer to start timer
> set theChars to every character in inText
> set theCharsRef to a reference to theChars
> repeat with aChar in theCharsRef # <- without Ref 10 times longer!
> set outText to outText & aChar
> end repeat
> set myTime to stop timer myTimer
> log outText
> myTime
>
> Ergebnis:
> 143.58203125
>
> 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.
In 2nd edition it's pages #382 & 383 (in other editions, search for "Speed > List Access"
Yvan KOENIG (VALLAURIS, France) mercredi 1 février 2012 11:42:47
_______________________________________________
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