Thursday, February 25, 2021

#1 2021-01-17 03:21:55 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 787

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.

Applescript:

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

Last edited by peavine (2021-01-17 03:36:22 pm)


2018 Mac mini - macOS Catalina

Offline

 

#2 2021-01-17 03:52:26 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Re: Scripting bridge timing tests

If I understand 'isUbiquitousItemAtURL' method is for target for iCloud...
If the folder of files are not target to iCloud it will return false.

To test your code theResult return true when the target was iCloud.

Only like to point that out.

To check if a target file have been uploaded to iCloud we could use
NSURLUbiquitousItemIsUploadedKey attribute


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#3 2021-01-18 10:21:19 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 787

Re: Scripting bridge timing tests

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.

Applescript:

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

Last edited by peavine (2021-01-18 10:36:57 am)


2018 Mac mini - macOS Catalina

Offline

 

#4 2021-01-18 11:40:59 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Re: Scripting bridge timing tests

The good thing about Finder is... put back from trash is working.
Its not always the case when we choose to do it with ASObjC.

If we need a reference to the location of deleted items smile


The purpose to study someone else art is not to add, its to make less more.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)