repeat with i in string optimization was Re: Understanding a repeat loop (strings/characters)
repeat with i in string optimization was Re: Understanding a repeat loop (strings/characters)
- Subject: repeat with i in string optimization was Re: Understanding a repeat loop (strings/characters)
- From: Paul Skinner <email@hidden>
- Date: Sun, 23 Feb 2003 11:35:12 -0500
On Sunday, February 23, 2003, at 09:06 AM, Emmanuel wrote:
At 1:35 AM +0000 23/02/03, has wrote:
Because your use of the 'repeat with foo in bar' structure implies
that
bar is a list that AS can repeat through. It's not; but certain data
types can be coerced to a list; so AS does this silently.
Incorrect. The 'repeat with itemRef in val' structure iterates over
an object's items. Both strings and lists have 'item' elements, so
either may be used here. And since in strings 'item' is synonymous
with 'character', what you end up with is a reference to each
character.
Surprising but obviously true: the behavior of the loop won't depend
on what the text item's delimiters are, thus it looks like the string
is never made into a list (which would involve the t.i.ds).
Emmanuel
I see that in the raw apple event. So I concede has's point. There is
no coercion to list.
So, what does this mean to me?
It means that I can pass a string of length 5000 characters, and I
won't have a problem addressing every character despite the 4069+/-
item limit on text to list conversion. Nice!
What about speed?
Well! Looping through a 2560 character string in both manners yields
some very interesting results. 100X+/- speed differences!
repeat with x in t-- t is a 2560 character string.
copy contents of x to a
end repeat
--> 0.0595 seconds
repeat with x in t-- t is a 2560 item list
copy contents of x to a
end repeat
--> 6.3498 seconds
It's even five+/- times faster than looping through a reference to t.
Regardless of t's class.
repeat with x in (a reference to t) -- t is a 2560 character
string.
copy contents of x to a
end repeat
-->0.3214 seconds
repeat with x in (a reference to t) -- t is a 2560 item list.
copy contents of x to a
end repeat
--> 0.2711 seconds
Why hasn't this been clearly stated? It seems to be an excelent
optimization technique.
Paul Skinner
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.