Re: FYI:Timings of AppleScripts
Re: FYI:Timings of AppleScripts
- Subject: Re: FYI:Timings of AppleScripts
- From: Graff <email@hidden>
- Date: Fri, 06 Feb 2004 14:32:45 -0500
Hmm, how quick does this script run on your iTunes library? I'm using
the shell command "sort" to do the sorting and culling out the repeated
lines.
-----------
tell application "iTunes"
set albumList to album of every track of playlist "Library"
end tell
set filePath to ((path to desktop) as text) & "Test"
set theWriteFile to open for access filePath with write permission
set {oldDelims, AppleScript's text item delimiters} to {AppleScript's
text item delimiters, ASCII character 10}
write (albumList as text) to theWriteFile
set AppleScript's text item delimiters to oldDelims
close access theWriteFile
set filePOSIX to quoted form of POSIX path of filePath
set uniqueText to do shell script "sort -fu " & filePOSIX
do shell script "rm " & filePOSIX
set uniqueList to every paragraph of uniqueText
-----------
A good way to time these scripts is to use the shell command "time".
Run it from the terminal like this:
/usr/bin/time osascript /path/to/yourscript.scpt
Using this command to run the script I get a time of about 0.5 seconds
for my iTunes Library of about 1700 songs.
- Ken
On Feb 6, 2004, at 12:16 PM, Mark Dawson wrote:
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.
_______________________________________________
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.