On 2005, Mar 21, , at 7:59 PM, Martin Fuhrer wrote:
I want to create a list of items, eg. a list of numbers from 5000 to
-5000. In Script Editor I can run the script:
set myList to {}
repeat with n from 1 to 10000
set end of myList to 5000 - n --SLOW!!
end repeat
Unfortunately, this is slow (about 30 seconds on my Powerbook G4 1.5
GHz). A much faster method is:
set myList to {}
set myListRef to a reference to myList
repeat with n from 1 to 10000
set end of myListRef to 5000 - n --FAST!!
end repeat
This takes less than a second when run in Script Editor.
Unfortunately, the second script will not work in an Applescript
Studio project. The loop produces the
"NSCannotCreateScriptCommandError (10)". The first script runs fine
in Applescript Studio, but once again, takes about 30 seconds to
complete.
If I recall correctly, 'my' worked for me:
repeat with n from 1 to 10000
set end of my myList to 5000 - n --FAST!!
end repeat
MarkB
Mark is right!
Here I run all 3 methods, plus the one suggested by D'Espinose-Garvey
(I believe) which turns out to be the fastest.
Using Smile I was able to time each run and get the times:
<script>
chrono
set myList to {}
repeat with n from 1 to 10000
set end of myList to 5000 - n --SLOW!!
end repeat
set a to chrono
set myList to {}
set myListRef to a reference to myList
repeat with n from 1 to 10000
set end of myListRef to 5000 - n --FAST!!
end repeat
set b to chrono
set myList to {}
set myListRef to a reference to myList
repeat with n from 1 to 10000
set end of my myList to 5000 - n --FAST!!
end repeat
set c to chrono
script f
property myList : {}
end script
repeat with n from 1 to 10000
set end of f's myList to 5000 - n --FAST!!
end repeat
set d to chrono
And other results which are consistent.
However, if I use 1000 instead of 10000 in the slow script the
improvement is of about 100 times. Not the same improvement in the
other methods which are still much faster
--{0.245663000009, 0.009181000001, 0.010443999985, 0.008418999991}