FYI:Timings of AppleScripts
FYI:Timings of AppleScripts
- Subject: FYI:Timings of AppleScripts
- From: Mark Dawson <email@hidden>
- Date: Fri, 6 Feb 2004 09:16:33 -0800
I thought I'd pass on the results of my question about the different
scripting examples, as changing code made such a HUGE difference. I
timed various code options, to see what the difference would be. My
main conclusion: AppleScript is SLOW! Any code that avoids repeated
AS calls will win (asking the program to handle the "every" clause
instead of doing it inside a repeat loop made the most difference).
Hope this helps others--it was sure a big surprise to me that different
approaches could make such a difference in execution speed!
On my dual 1 GHz G4, 10.3.2, 5000 track iTunes library:
The most efficient script took 18 seconds to complete; the worst 183
seconds (10x difference!)
The below didn't seem to effect the time:
set theCount to count of albumlist
repeat with i from 1 to theCount
vs
repeat with i from 1 to count of albumlist
or
copy thisAlbum to end of albumAddList
copy (thisArtist & ";" & thisAlbum) to end of albumArtistList
vs
set end of albumAddList to thisAlbum
set end of albumArtistList to thisArtist & ";" & thisAlbum
Doing a "set thisAlbum to item i of albumlist" and using "thisAlbum"
instead of using "item i of albumlist" in the 3 spots saved 2 seconds
(20 seconds->18 seconds).
Doing a "set thisArtist" and using "thisArtist" the one time didn't
seem to change the time any (vs just using the "item i of artistList"
directly).
Grabbing the album list from the program (vs inside the repeat loop)
dropped the time from 183 seconds down to 32 seconds. Moving the "set
thisArtist to item i of artistList" line from after the "set thisAlbum
to item i of albumlist" to after the "if" dropped the time down to 18
seconds.
So, in my case, there was a 10x (183 seconds->18 seconds) improvement
in speed by adjusting the algorithm!
As an FYI, here is my "final" AppleScript that grabs both the artist
and album, with no duplicate albums, and executes in 18 seconds (on a
5000 track iTunes library)
tell application "iTunes"
set albumArtistList to {}
set albumAddList to {}
set albumlist to (album of every track of library playlist 1) as list
set artistList to (artist of every track of library playlist 1) as list
set theCount to count of albumlist
repeat with i from 1 to theCount
set thisAlbum to item i of albumlist
if albumAddList does not contain thisAlbum then
set thisArtist to item i of artistList
set end of albumAddList to thisAlbum
set end of albumArtistList to thisArtist & ";" & thisAlbum
end if
end repeat
end tell
albumArtistList
_______________________________________________
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.