Speed & Choose from list
Speed & Choose from list
- Subject: Speed & Choose from list
- From: Stephen Swift <email@hidden>
- Date: Tue, 07 May 2002 21:22:24 -0400
Recently I posted a question about how to determine what item the user
choose given a list and how to make the script perform another action (in
this case displaying a different string). Thanks for all your suggestions!
Being a speed freak... I ran a series of tests to determine which was the
fastest. As I discovered, all were fast and there wasn't much difference.
However, some of the options did stand out as requiring a greater amount of
time than the others.
The test:
Each script had to run through a list of 60 options and pick the option the
user selected.
The script then had to find the corresponding string (#60) to display and
create a dialog box.
The results:
Using TIDs to determine the user choice by breaking up the list into one
long string separated by "***" proved to be the fastest option (4 ticks).
Using a repeat loop proved to be the second fastest choice (6 ticks).
Because it was the simplest code and there wasn't much speed difference. It
was the script I ended up using.
The on StrIndex(i) handler trick that Arthur showed us ran in 9 ticks. The
only drawback to that was that every handler in the list had to run when the
list was set. And if you had a long list of 60 items, it was a pain to set
up because each option had to have a unique number attached to it.
Although the BCSearch (Binary "Contains" Search) wasn't meant to be a
"choose from list" script but I wanted to see how it faired. It ran in 10
ticks. This led to me wondering whether if a standard repeat loop to
determine what item # a certain item is wouldn't do just as good of a job.
Instead of creating a normal list, but creating a long string divided by
returns took 40 ticks to complete. This method used the offset command to
calculate the results. Had it used TIDs, it probably would have been
faster.
The slowest by far (at 90 ticks) was setting up a record that contained both
the "choose list option" as the label and "display dialog string" as the
value. This is because the script had to convert the labels over to strings
to be used in the choose from list dialog. It was also cumbersome to set up
a long list.
But as most agreed, all options were fast and easy to use. I just found it
interesting trying to figure out which was the most efficient if it had to
run through a huge huge list (looking at the amount of code & speed).
If you can't remember the scripts I'm referring to, below are excerpts
containing the basic idea of the script:
<tids>
set someItem to choose from list theList
set AppleScript's text item delimiters to "***"
set vTabStr to theList as string
set AppleScript's text item delimiters to (someItem's item 1)
set vTabStr1 to (vTabStr's text item 1)
set AppleScript's text item delimiters to "***"
set x to count (vTabStr1's text items)
</tids>
<repeat loop>
on UniqueIndex(aList, anItem)
repeat with i from 1 to (count aList)
if anItem = item i of aList then return i
end repeat
return 0
end UniqueIndex
</repeat loop>
<StrIndex>
on StrIndex(i)
set s to return
repeat i times
set s to s & space
end repeat
end StrIndex
set a to {"Item 1" & StrIndex(1), "Item 2" & StrIndex(2)}
</StrIndex>
<BCSearch>
on BCSearch(a, v)
tell a to if it contains {v} then
set l to 1
set r to length
repeat until item l = v
set m to (l + r) div 2
if items l thru m contains {v} then
set r to m
else
set l to m + 1
end if
end repeat
return l
end if
return 0
end BCSearch
</BCSearch>
<long string>
set theDialog to (choose from list paragraphs 2 thru -2 of listStr)
if theDialog is false then error number -128
set chrOffset to the offset of (return & theDialog & return) in listStr
set listIndex to (count paragraphs of text 1 thru chrOffset of listStr) - 1
</long string>
<record>
set theList to {|Dialog Box 1|:"Good Morning", |Dialog Box 2|:"Good
Afternoon", |Dialog Box 3|:"Good Evening", |Dialog Box 4|:"Good Night"}
set theDialog to choose from list |Clean Labels|(theList)
|Find Value|(theDialog's item 1, theList)
</record>
Stephen Swift
email@hidden
-----------------------------------------------
AppleScript Guru - The Mac Observer
http://www.macobserver.com/tips/applescript
_______________________________________________
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.