More on enhanced applet execution speed with the "run script" command

I started a post a couple of weeks ago (http://bbs.applescript.net/viewtopic.php?id=19863) after making the empirical observation that an AppleScript applet ran 7 times more quickly when executed via the command run script file <applet.app> than when executed via the command tell application “Finder” to open <applet.app> (or equivalently, by double-clicking <applet.app>'s icon in the Finder). The run script effect came to my attention because the applet I was working on, namely resizing a TextEdit window to accommodate the window’s text neatly, provided visually obvious feedback about execution speed. Thanks to excellent input from several contributors, particularly Nigel and Eelco, we narrowed the speed-enhancing effect of the run script command to its particularly efficient handling of interapplication communications (ie, tell statements) relative to what happens when applets are executed conventionally via the Finder open command (= double clicking). It also was found to be OS X-specific, and more pronounced in Tiger than in Jaguar.

In view of observed speed increases of 20-fold or more under select circumstances (as described in the earlier post), I tested the run script effect on a broader range of applications and commands. The results are in the table at the end of this post. One notable finding was that the run script effect is variable among applications, and among commands within applications. The effect is particularly pronounced for window resizing, moving data into and out of the clipboard, and getting application and file attributes and properties. In these cases, run script executes anywhere from about 3 to 10 times more quickly than Finder open. At the other extreme, no observable speed increase was noted for emptying the trash with Finder, mounting/unmounting volumes, or for the Unix actions mediated by do shell script. At an intermediate level, image resizing (tested for Photoshop and Image Events), application launching and quitting, the time taken to create the overhead of a do shell script command, and individual file duplication, moving, and renaming in the Finder, all saw about a 1.5-fold increase in execution speed. Bulk file duplication and moving in the Finder (ie, tell application “Finder” to duplicate/move every file …") showed only about a 1.1-fold (10%) increase in execution speed.

I then tested the effect in the “real world” on several complex scripts of mine that take time to execute and involve numerous tell statements and communications between applications. The bottom line: 1.5- to 4-fold increases in execution speed were observed consistently across scripts, depending of course on the specifics of the code. It may not seem like much, but for slow-running applets, those speed enhancements are a noticeable and welcome improvement.

Having said all this, I would like to invite anyone to expand the testing of the run script effect on various applications and commands. Posts complaining about slow execution speed are common, and it would be nice to hear if significant speed enhancements can be achieved with this simple technique. For example, there was a lively discussion this past week on slow execution of a large script (http://bbs.applescript.net/viewtopic.php?id=9200), and it would be interesting to see how much more quickly it would execute with run script.

For those interested in trying it out, simply save your script either as a compiled script (call it your_script.scpt) or an applet (call it your_script.app), then execute that script via a separate applet (which can be executed conventionally by double-clicking) consisting of the single command:


run script file --"full:path:to:your_script.scpt or your_script.app"--
-- or --
run script --alias to your_script.scpt or your_script.app--

If you wish to keep all the code in a single double-clickable applet, simply convert your code into a script object, and run script the script object as follows:


script user_chosen_script_name
-- ... your code here ... --
end script
run script user_chosen_script_name

bmose


                                                                                                                        Execution speed
                                                                                                                       with [i]Finder open[/i]
                                                                                                                            divided by
                                                                                                                        execution speed

Application Task with run script

39 different apps (results averaged) get name * 9.87
System Events → TextEdit process resize window 8.47
System Events → Finder process resize window 7.58
System Events → Script Editor process resize window 8.56
TextEdit resize window 4.32
Finder resize window 3.65
Script Editor resize window 4.23
System Events → TextEdit process copy to then paste from the clipboard 6.35
System Events → TextWrangler process copy to then paste from the clipboard 5.61
Standard Additions → TextEdit app copy to then paste from the clipboard 1.90
Standard Additions → TextWrangler app copy to then paste from the clipboard 3.63
Standard Additions get info for file 4.31
System Events → 104 different get title * 2.33
appl processes (results averaged)
Finder get name of file 3.08
Finder duplicate file 2.16
Finder move file 1.32
Finder set name of file 1.32
Finder duplicate every file (1000 files) 1.16
Finder move every file (1000 files) 1.15
Photoshop resize image (500 images) 1.62
Image Events resize image (500 images) 1.53
24 different apps (results averaged) quit 1.51
90 different apps (results averaged) launch 1.27
Standard Additions do shell script “” ** 1.46
Standard Additions do shell script “echo ABC” 1.62
Standard Additions do shell script “ls “l” 1.25
Standard Additions do shell script “ps -acwx | grep ‘Finder’|
egrep -o ‘^ +[0-9]+’ | egrep -o ‘[0-9]+’” *** 1.10
Standard Additions do shell script "diskutil mount/unmount " 1
Finder empty trash(2500 files) 1

  • an example of getting a property value
    ** null string after “do shell script” tests time
    of setting up the shell
    *** parses “ps” output and returns process
    id number for process “Finder”