• 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
Re: Offset vs. Repeat loops
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Scriptable Email apps
      • From: "Lawrence M. Borland, M.D." <email@hidden>
    • Scriptable Email Application
      • From: "Lawrence M. Borland, M.D." <email@hidden>
References: 
 >Offset vs. Repeat loops (From: Greg Strange <email@hidden>)

  • Prev by Date: Re: applescript-users digest, Vol 2 #841 - 13 msgs
  • Next by Date: Quit Eudora
  • Previous by thread: Offset vs. Repeat loops
  • Next by thread: Scriptable Email Application
  • Index(es):
    • Date
    • Thread