Re: Offset vs. Repeat loops
Re: Offset vs. Repeat loops
- Subject: Re: Offset vs. Repeat loops
- From: Andy Wylie <email@hidden>
- Date: Sat, 07 Jul 2001 14:21:53 +1200
on 7/7/01 11:47 am, Greg Strange at email@hidden wrote:
>
So,
>
>
Michelle posted an excellent handler for changing strings form upper to
>
lowercase and vice versa. I thought it good but I thought that getting the
>
offset of one in the other string faster given that you don't have to loop
>
through every character. The difference was amazing. Michelle's mod
>
finished 1000 iterations in 518 ticks (on average through five overall
>
runs). Mine posted below did 1000 iterations in 2321 ticks (on average
>
through five overall runs)!
>
>
Why is offset so much slower than repeat loops? That seems
>
counterintuitive. (Mind the wraps!)
>
>
global lowercasestring, uppercasestring, outString
>
set outString to ""
>
set lowercasestring to "abcdefghijklmnopqrstuvwxyz"
>
set uppercasestring to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>
>
to uppercase(theText)
>
repeat with i in theText
>
try
>
set myOffset to the offset of i in lowercasestring
>
set outString to outString & character myOffset of
>
uppercasestring
>
on error
>
set outString to outString & i
>
end try
>
end repeat
>
return outString
>
end uppercase
>
>
>
to lowercase(theText)
>
repeat with i in theText
>
try
>
set myOffset to the offset of i in uppercasestring
>
set outString to outString & character myOffset of
>
lowercasestring
>
on error
>
set outString to outString & i
>
end try
>
end repeat
>
return outString
>
end lowercase
if you do it like this it's twice as fast as looping...
property lowList : {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
property upList : {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
property offstring : "abcdefghijklmnopqrstuvwxyz"
repeat with i in theText
try
set outList to outList & (upList's item (offset of i in
offstring))
on error
set outList to outList & i
end try
end repeat
_____________________________ Andy