Scripting bridge timing tests

I have a number of scripts that generate an alias list, which is then submitted to an ASObjC handler for processing. There are often at least two approaches by which this can be done: the first is to explicitly convert the aliases to a POSIX path and then to a URL; the second is to let the scripting bridge handle the conversions.

To help determine if one approach is better than the other, I ran some timing tests. The results were:

Explicit Conversion: 0.510 second

Scripting-bridge conversion: 0.280 second

I’ve included the timing test below. I used the isUbiquitousItemAtURL method because I wanted something that returned a boolean and required a url. The script includes both of the code segments being tested and one has to be commented out to test the other. I’m new to ASObjC and if my testing methodology or understanding of this whole issue is faulty, please let me know.

use framework "Foundation"
use scripting additions

-- untimed code
set testFolder to (choose folder)
tell application "Finder"
	set theFiles to every file of the entire contents of testFolder as alias list -- contains 443 files
end tell

-- start time
set startTime to current application's CFAbsoluteTimeGetCurrent()

-- timed code

-- explicit conversion
set fileManager to current application's NSFileManager's defaultManager()
repeat with aFile in theFiles
	set thePath to (current application's |NSURL|'s fileURLWithPath:(POSIX path of aFile))
	set theResult to (fileManager's isUbiquitousItemAtURL:thePath)
end repeat

-- scripting bridge
# set fileManager to current application's NSFileManager's defaultManager()
# repeat with aFile in theFiles
# 	set theResult to (fileManager's isUbiquitousItemAtURL:aFile)
# end repeat

-- elapsed time
set elapsedTime to (current application's CFAbsoluteTimeGetCurrent()) - startTime
set nf to current application's NSNumberFormatter's new()
nf's setFormat:("0.000")
set elapsedTime to ((nf's stringFromNumber:elapsedTime) as text) & " seconds"

-- result
elapsedTime

Thanks Fredrik71 for looking at my post.

I ran additional timing tests, which involved deleting 100 files, and the results were comparable to my earlier tests.

Explicit conversion - 0.120 second

Scripting-bridge conversion - 0.065 second

Just to establish a baseline of sorts, I ran the same timing test but with the Finder’s delete command, which moves the files to the trash rather than deletes them, and the timing result was 0.350 second.

use framework "Foundation"
use scripting additions

-- untimed code
set testFolder to (choose folder)
tell application "Finder"
	set theFiles to every file of the entire contents of testFolder as alias list -- 100 files
end tell

-- start time
set startTime to current application's CFAbsoluteTimeGetCurrent()

-- timed code
set fileManager to current application's NSFileManager's defaultManager() -- 0.065 second
repeat with aFile in theFiles
	(fileManager's removeItemAtURL:aFile |error|:(missing value))
end repeat

# set fileManager to current application's NSFileManager's defaultManager() -- 0.120 second
# repeat with aFile in theFiles
# 	set theURL to (current application's |NSURL|'s fileURLWithPath:(POSIX path of aFile))
# 	(fileManager's removeItemAtURL:theURL |error|:(missing value))
# end repeat

-- elapsed time
set elapsedTime to (current application's CFAbsoluteTimeGetCurrent()) - startTime
set nf to current application's NSNumberFormatter's new()
nf's setFormat:("0.000")
set elapsedTime to ((nf's stringFromNumber:elapsedTime) as text) & " seconds"

-- result
elapsedTime