Offset vs. Repeat loops
Offset vs. Repeat loops
- Subject: Offset vs. Repeat loops
- From: Greg Strange <email@hidden>
- Date: Fri, 06 Jul 2001 18:47:17 -0500
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