Hi, Adam.
Access to the items or properties of a list is faster if you use a reference to identify the list rather than a simple variable. Assigning the list to a property of a script object allows you to use a reference expression like ‘BL’s Big_List’ rather than just, say, ‘Big_List’. The script object itself doesn’t make access faster, it simply allows a reference expression to be written into the script. This is particularly useful inside a handler, where the list variable may be local and thus unreferenceable. At the top level of a script, though, properties and variables that aren’t explicitly declared local belong to the script itself and so you can use the keyword ‘my’ to set up the reference:
set Big_List to { 1500-item list } -- A list assigned to a top-level variable.
repeat with i from 1 to 1500
item i of my Big_List -- 'my Big_List' is a reference.
end repeat
The speed advantage with a reference only comes when accessing things belonging to the list. Commands acting on the list itself ” such as ‘count’, ‘contains’, ‘is in’, etc. ” are often a trifle slower if the list is referenced.
In your script, there are other things too that can be done to improve the speed ” principally, counting Big_List only once and assigning the result to a variable before the repeats. (Otherwise it’ll be counted 1501 times, always with the same result!)
In extreme cases, you can sometimes squeeze out an extra fraction of a second or so by doing positive tests instead of negative ones.
-- Negative test:
if (i is not in the BL's index_list) then
-- Blah blah blah
end if
-- Positive test:
if (i is in the BL's index_list) then
else
-- Blah blah blah
end if
I would think you could leave out the ‘as text’ coercion too.
If you know that all your text is the same case, or that the characters involved only have one case, putting the whole thing in a ‘considering case’ block can also give quite a boost. It saves the internal comparison routines having to check and allow for case and casedness.
Your central 'if . else if ’ block is a bit perplexing as the action is the same in both parts. As the script works correctly, I supposed you’re looking for an ‘or’ condition. It shouldn’t make any difference to the speed, but it’ll make the script a little shorter.
The above is all quoted from past experience. I haven’t tested it today to see what actual improvement it makes to your script’s speed!
set Big_List to { a list of 1500 words }
set index_list to {}
set sorted_list to {}
set t1 to GetMilliSec
set BL_count to (count Big_List) -- Count Big_List just once, before the repeats.
repeat BL_count times
set the low_item to ""
repeat with i from 1 to BL_count
-- Positive test, and no reference when accessing the list itself.
if (i is in the index_list) then
else
-- A reference when accessing an item in the list.
set this_item to item i of my Big_List
if (the low_item is "") or (this_item comes before the low_item) then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
-- References while setting the lists' ends.
set the end of my sorted_list to the low_item
set the end of my index_list to the low_item_index
end repeat
set t2 to GetMilliSec
t2 - t1