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.
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 {}
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
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:
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
All the best
Thomas