There have been a number of threads of late that dealt with methods which can be used to improve the time it takes to read from and write to large lists. I just finished some additional testing on this topic and, FWIW, thought I would include the results here. I have not copied each script below and instead have included only the code being timed. The names I use for the two script-object methods may not be technically correct but I couldn’t think of anything else.
NO SPEED ENHANCEMENT - 142 seconds
use framework "Foundation"
use scripting additions
-- untimed code
set aList to {"Abraham", "Barbara", "Charles"}
repeat 14 times
set aList to aList & aList
end repeat
set aList to aList & "Extra"
set aList to aList & {"Abraham", "Barbara", "Charles"}
-- start time
set startTime to current application's CFAbsoluteTimeGetCurrent()
-- timed code
set newList to {}
repeat with i from 1 to (count aList)
if item i of aList = "Extra" then
exit repeat
else
set end of newList to item i of aList
end if
end repeat
-- elapsed time
set elapsedTime to (current application's CFAbsoluteTimeGetCurrent()) - startTime
set nf to current application's NSNumberFormatter's new()
nf's setFormat:("0.000")
set elapsedTime to ((nf's stringFromNumber:elapsedTime) as text) & " seconds"
-- result
elapsedTime
A REFERENCE TO - 1.187 seconds
-- timed code
set aListRef to a reference to aList
set newList to {}
set newListRef to a reference to newList
repeat with i from 1 to (count aListRef)
if item i of aListRef = "Extra" then
exit repeat
else
set end of newListRef to item i of aListRef
end if
end repeat
EXPLICIT SCRIPT OBJECT - 0.802 seconds
-- timed code
main(aList)
on main(aList)
script o
property aListRef : aList
property newList : {}
end script
repeat with i from 1 to (count o's aListRef)
if item i of o's aListRef = "Extra" then
exit repeat
else
set end of o's newList to item i of o's aListRef
end if
end repeat
end main
IMPLICIT SCRIPT OBJECT - 0.777 seconds
-- timed code
set newList to my {}
repeat with i from 1 to (count my aList)
if item i of my aList = "Extra" then
exit repeat
else
set end of my newList to item i of my aList
end if
end repeat
A few comments:
- The basic procedure used to time the scripts was written by Nigel.
- The code under the comment “elapsed time” was written by Shane.
- The source and output lists contain 49,156 and 49,152 items.