• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Strings & Loops
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Strings & Loops


  • Subject: Strings & Loops
  • From: Thomas Fischer <email@hidden>
  • Date: Wed, 01 Feb 2012 11:11:12 +0100

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

All the best
Thomas


 _______________________________________________
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

  • Follow-Ups:
    • Re: Strings & Loops
      • From: KOENIG Yvan <email@hidden>
  • Next by Date: Re: Strings & Loops
  • Next by thread: Re: Strings & Loops
  • Index(es):
    • Date
    • Thread