RE: Faster List Checking
RE: Faster List Checking
- Subject: RE: Faster List Checking
- From: "Frank W. Walker" <email@hidden>
- Date: Wed, 28 May 2003 17:20:16 -0400
- Thread-topic: Faster List Checking
This is the kind of basic knowledge I am sadly lacking and that I was hoping to get when I tossed my fishing line in the Applescript Users pool. Thanks!
Frank
>
----------
>
From: Andrew Oliver
>
Sent: Wednesday, May 28, 2003 5:15 PM
>
To: Frank W. Walker; Applescript Users (E-mail)
>
Subject: Re: Faster List Checking
>
>
A couple of things come to mind here, some general, and some specific to
>
your script.
>
>
General observations:
>
>
In the first block:
>
>
> repeat with i from 1 to (count text items in errorlist)
>
> if text item i of errorlist = graphic then
>
> set knownerror to "YES"
>
> end if
>
> end repeat
>
>
You can potentially save a lot of time by adding an 'exit repeat' after the
>
'set knowneerror to "YES".
>
What this does it terminates the repeat loop at the first match. In its
>
current incarnation you iterate through the entire list every time, even if
>
the first item in the list matches. In this case you don't care WHICH item
>
matches, just that any item does, so there's no point in checking past the
>
first match.
>
>
Also, the line:
>
>
>repeat with i from 1 to (count text items in errorlist)
>
>
Can be optimized to:
>
>
> set numItems to count text items in errorList
>
> repeat with I from 1 to numItems
>
>
In the original form, the script will calculate the 'count text items in
>
errorlist' each time it goes through the loop.
>
By precalculating the number of items in the list you save time.
>
You can only use this technique when you know the list won't change. If
>
you're adding or subtracting items from the list you need to recalculate the
>
number of items in the list on each iteration, but in your script there's no
>
need.
>
>
You'd also save a lot of time by preconverting the errorList string into a
>
list rather than re-splitting it into 'text items' each time you iterate
>
through.
>
>
However, all the above is irrelevant in this case because here's the
>
ultimate kicker:
>
>
Since you only care if graphic appears in the errorList, you can say:
>
>
> if errorList contains graphic then
>
> set knownerror to "YES"
>
> end if
>
>
This eliminates the need to delimit the string, walk the loop, etc., etc.
>
>
Andrew
>
:)
>
>
On 5/28/03 11:15 AM, "Frank W. Walker" <email@hidden> wrote:
>
>
> The recent discussion about lists makes me wonder if I could improve the speed
>
> of my latest script.
>
> In my script, there are two lists, text delimited by ", ":
>
> "errorlist" contains known instances of missing items (graphics).
>
> "logolist" contains all items (graphics) which have previously been found.
>
>
>
> The following lines check the list "errorlist" for the text item contained in
>
> the variable "graphic":
>
>
>
> set AppleScript's text item delimiters to {", "}
>
> repeat with i from 1 to (count text items in errorlist) --checks error list
>
> for current logo name
>
> if text item i of errorlist = graphic then
>
> set knownerror to "YES"
>
> end if
>
> end repeat
>
> set AppleScript's text item delimiters to {""}
>
>
>
>
>
> The next part checks the list "logolist" for the text item contained in the
>
> variable "graphic".
>
> If it finds the text item, it sets the variable "templatename" to a certain
>
> string.
>
>
>
>
>
> set AppleScript's text item delimiters to {", "}
>
> repeat with i from 1 to (count text items in logolist)
>
> if text item i of logolist = graphic then --checks logolist for previous
>
> use of graphic
>
> set templatename to "LOGO" & graphic
>
> exit repeat
>
> end if
>
> end repeat
>
> set AppleScript's text item delimiters to {""}
>
>
>
>
>
> Can I make these faster?
>
>
>
> TIA
>
> Frank
>
> _______________________________________________
>
> 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.>
_______________________________________________
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.