Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fastest Method to Append to a List




On Mar 22, 2005, at 0:14, Mark Butcher wrote:


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

{a, b, c, d}
-->{25.445097999997, 0.286832000013, 0.198849000008, 0.160601999989}
-->{38.356083999999, 0.155960000004, 0.157903999992, 0.139113999991}
</script>

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}


Deivy

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-studio mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-studio/email@hidden

This email sent to email@hidden
References: 
 >Fastest Method to Append to a List (From: Martin Fuhrer <email@hidden>)
 >Re: Fastest Method to Append to a List (From: Mark Butcher <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.