In today’s Code Exchange, SwissalpS used this handler to list the files he was going to work with:
to makeFileList(startFolder)
tell application "Finder"
set fileList to startFolder's files
set outList to {}
repeat with thisItem in fileList
set outList's end to thisItem as alias
end repeat
end tell
return outList
end makeFileList
I usually use this (using the same variables):
to makefilelist2(startFolder)
tell application "Finder" to try
return files of entire contents of startFolder as alias list
on error -- only one file (a bug in "as alias list")
return files of entire contents of startFolder as alias as list
end try
end makefilelist2
and I wondered which was faster. Turns out for a Garvey “lotsa(many)” run of 100 on my pictures folder the results on my machine are:
{t1, t2, t1 / t2, t2 / t1} → {45.024, 63.35, 0.710718232044, 1.407027363184}, i.e. SwissalpS’ version is 40% faster.
very interesting, normally I don’t use a routine to coerce a file specifier list to an alias list
I coerce the specifiers temporarily in the repeat loop e.g.
set startFolder to choose folder
tell application "Finder"
set fileList to startFolder's files
repeat with oneFile in fileList
set {name:Nm, name extension:Ex} to info for oneFile as alias
.
end repeat
end tell
If there are many files in subfolders of your test folder, then the test seems a bit unfair since the second one is processing the entire contents of the test folder and the other is only processing the immediate contents of the test folder. Maybe that is causing your timing difference.
Note: For me, files of entire contents of x generates an error when x refers to an folder that does not contain any files (it is empty or only has subfolders that are likewise empty or only have subfolders). Also, there is a copy/paste bug in the error handler code: “entire contents of f” should be “entire contents of startFolder”.
Error corrected - thanks Chrys. Also the point about embedded folders is a good one in this comparison – I’ll repeat the tests being more careful about the contents of the folder chosen. That’s what you get for doing tests late in the day. :rolleyes:
Chrys hit the nail on the head. When I run the two handlers against a folder with only one level, my version (which I think only works in Tiger, BTW) is 5.6 times faster than SwissalpS’ approach.
Sorry for the who-ha :rolleyes:
set Ratios to lotsa(500) -- the number of runs
on lotsa(many)
-- Any preliminary or common values here.
set startFolder to alias "pathToFolderFullofPixButNoFolders"
-- Dummy loop to absorb a small observed
-- time handicap in the first repeat.
repeat many times
end repeat
-- Test 1.
set t to GetMilliSec
repeat many times
-- First test code or handler call here.
my MakeFileList(startFolder)
end repeat
set t1 to ((GetMilliSec) - t) / 1000
-- Test 2.
set t to GetMilliSec
repeat many times
-- Second test code or handler call here.
my makeFileList2(startFolder)
end repeat
set t2 to ((GetMilliSec) - t) / 1000
-- Timings.
return {t1, t2, t1 / t2, t2 / t1} --> {43.55, 7.741, 5.625888128149, 0.177749712974}
end lotsa
to MakeFileList(startFolder) -- SwissalpS approach with return statement removed
tell application "Finder"
set fileList to startFolder's files
set outList to {}
repeat with thisItem in fileList
set outList's end to thisItem as alias
end repeat
end tell
end MakeFileList
to makeFileList2(startFolder) -- My usual approach with return statement removed.
tell application "Finder" to try
files of entire contents of startFolder as alias list
on error -- only one file (a bug in "as alias list")
files of entire contents of startFolder as alias as list
end try
end makeFileList2