Why do Applescript applications run 60% slower than script in editor

I have a bench mark script that I have used on the past several generations of iMac hardware and systems to test speed. With Leopard, I have noticed that when I adjust the script to show 100% on the compiled application, the source script run through script editor runs faster, producing a result of 240%. I always thought that complied script should run faster. Why is this happening?

iMac 24" 2.8GHz Intel, Mac OS X (10.5)

Posted on Mar 29, 2008 6:43 PM

Reply
15 replies

Mar 31, 2008 3:27 AM in response to Al Gunther

I posted the script in the thread Hiroto referenced. Because of your response, I went back and had another look at Hiroto's "fix," with these results:

Without Hiroto's "fix" the script editor version of the repeat sequence runs in ~ 164 ticks, whereas the applet version takes ~544 ticks.

With Hiroto's "fix" the corresponding numbers are ~165 and ~165 ticks (the fix obviously is 100% in this case).

The "fix" I applied was exactly this:

*Added at beginning of script:*
script TestRunTime

*Added at end of script:*
return me
end script
set TestRunTime to run script TestRunTime

There appear to be two differences in your case:

1) Your script
2) Your OS

neither of which I have -- so I can't play with those.

However, you could try my script, with and without the "fix," and see how it fares with Leopard.

For clarity, I've posted the script here. However, note these things:

1) I've removed my timing mechanism, which was based on the scripting addition "the ticks" from Jon's Commands (this won't work on an Intel MAC). But, at its fastest, the script takes about 3 seconds to run, and, at its slowest about 9 seconds -- so you should be able to gauge these differences with a watch or the system clock.

2) The script needs a picture file. I've deleted the one I used. You need to insert the path to a .jpg file (for example) that's on your computer. The place you need to do this is clearly indicated in the script I've posted.

--BEGIN SCRIPT

--(uncomment the following line, and 3 lines at the end of the script, to implement Hiroto's "fix")
--script TestRunTime
tell application "Finder" to set PictPath to "*INSERT PATH TO YOUR .jpg FILE HERE*"
tell application "Preview"
activate
open PictPath
tell application "System Events"
repeat 20 times
tell process "Preview"
tell menu bar 1
tell menu bar item "Tools"
tell menu "Tools"
click menu item "Flip Horizontal"
end tell
end tell
end tell
end tell
delay 0.1
end repeat
end tell
close window 1 saving no
end tell
--(uncomment the next 3 lines, and 1 line at the beginning of the script, to implement the fix)
--return me
--end script
--set TestRunTime to run script TestRunTime

--END SCRIPT

If you don't see a roughly 3x speed improvement in the applet with the "fix," that would suggest that Leopard and Tiger respond to the fix differently.

If you do see a roughly 3x improvement with the above script, that would suggest that your benchmark script doesn't respond to the fix similarly. Would it be possible to post that script?

(Of course, I'm assuming that the above script will basically run on your setup.)

I'm really interested in your results.

Mar 31, 2008 10:30 AM in response to osimp

Your script hangs up after the first rotation in Preview on my system. I have version 4.1 of Preview with Leopard and Intel which is not apparently scriptable. I tried to convert the script to run on GraphicConverter but could not get the GUI to work at all. But GC is very scriptable so I modified the script accordingly.

Runtime is 0.773 seconds normal, 0.682 seconds in 'run script' osax. This seems comparable to the improvement that I had in my own script.

The following is my version of your routine. (I ran it with a delay inserted and it does flip the photo 20 times.)


script o

set secs to getSec()
doRoutine()
set secs to getSec() - secs - 0.013 --editor
--set secs to getSec() - secs - 0.021 --application
if secs < 0 then set secs to secs + 60

tell application "TextEdit"
set dat to text of document 1
set text of document 1 to dat & "Seconds= " & secs & return
end tell

on doRoutine()
set PictPath to "Macintosh HD:Users:agunther:Desktop:Picture 3.jpg" as alias
tell application "GraphicConverter"
activate
open PictPath
repeat 20 times
mirror window 1 in horizontal
end repeat
close window 1 saving no
end tell
end doRoutine

on getSec()
try
do shell script "defaults read b"
on error errorText
-- errorText is current date log
text 18 thru 23 of errorText
-- extracts seconds portion in ##.###
end try
end getSec

end script

run script o -- use 'run script' osax to run your main code

Apr 1, 2008 3:48 AM in response to Al Gunther

It's true that Preview is not normally scriptable beyond basic commands. But System Events is, and that's what implements the GUI.

In my script, the delay (line 17) is essential; without it, the "Apple" menu inexplicably opens at some point into the script.

I found that a minimum 0.1 second delay would prevent that. It would be interesting to see if increasing the delay would prevent the hang on your machine. A small increase might do it, but I'd start with 2 or 3 seconds and and work my way down toward the 0.1 value.

If you can make it work with a very small delay, then we'd be comparing apples with apples. Remember to compare applet versions of the script with and without the fix. The script version doesn't seem to be affected by the fix.

Apr 1, 2008 8:33 AM in response to osimp

osimp,

I found that your script on my machine would run consistently with a 0.4 delay, but not with a 0.3 delay. "Close window saving no" would require GUI scripting on my system so I just disabled it. Here are my results:

Seconds= 8.586 in editor using 'run script' osax
Seconds= 8.48 above repeated

Seconds= 8.77 in editor NOT using 'run script' osax
Seconds= 8.755 above repeated

Seconds= 8.729 as Applet using 'run script' osax
Seconds= 8.73 above repeated

Seconds= 16.027 as Applet NOT using 'run script' osax
Seconds= 16.493 above repeated

The difference in Applet times was a huge surprise to me since 20 x 0.4 = 8 seconds of the time is used for delays in either case. Subtracting that out would make the difference much greater.

Apr 1, 2008 9:09 AM in response to Hiroto

Hiroto,

It's amazing to me that your predictions are quite true. (I'm not amazed at your ability to predict, only that it actually happens.)

I ran a test using my Graphics Converter routine since it contains no delays, with the following results:

Seconds= 1.337 Applet not bundled
Seconds= 1.671 (repeated)

Seconds= 0.875 Applet using 'run script' osax not bundled
Seconds= 0.856


Seconds= 1.792 Applet bundled
Seconds= 1.46


Seconds= 0.681 Applet bundled using 'run script' osax
Seconds= 0.698

Apr 1, 2008 10:09 AM in response to Hiroto

Hiroto,

In looking back at my results testing, I see now that if I include the 'run script' osax and the bundle application option, the script run times are the same as with the script editor. So, the answer to my question is that I have not been doing it to best advantage. I can accept that, so I am marking this question solved.

Thanks to both you and osimp

Apr 1, 2008 5:07 PM in response to Al Gunther

Since you consider the matter solved, ALL the credit for that goes to Hiroto.

I continue to be amazed at Hiroto's success in solving "OS X problems" while using 9.1; I've learned not to ignore his suggestions, even if they completely baffle me.

On another subject: I had not previously seen (or thought of) your approach to script timing, but I think it will be useful if I get involved with Intel machines. "The ticks" scripting addition probably yields somewhat more accurate* results but it won't work with Intel.

* I ran a couple comparison tests, using a delay of 1 second or 10 seconds as the entire code, repeating 10 to 100 times. With the ticks, there occurred only one value of deviation, 16 msec, and that occurred infrequently. With the defaults approach, there were numerous deviations, typically 15 to 45 msec. There's certainly more to judging accuracy that these numbers, but both seem to be trustworthy solutions.

Apr 1, 2008 6:41 PM in response to osimp

osimp,

I'm not sure why ticks would be more accurate than the milliseconds from "the default" method

the script:
---------------
try
do shell script "defaults read b"
on error e
return e
end try
--------------
returns: "2008-04-01 18:26:19.729 defaults[20413:613]
Domain b does not exist"
--------------

The first part of this comes from the system clock and my routine only pickes up the millisecond portion of the time. The inaccuracy is the fact that the routine takes 120+ milliseconds for one call, depending on how it's run. I ran a loop of 1000 calls and came up with 12 seconds for bundled app and slightly more for running scrip through the script editor. So, that's where the .012 comes from that I subtract off the routine's time. (includes half the start call time and half the finish call time)

Do you account for the time it takes to read the number of ticks?

As to credit for answering the question, I didn't know how to give partial credit. You did contribute also. Thanks!

Al

Apr 4, 2008 6:57 PM in response to Hiroto

While the problem I was having arose from my compling for PPC instead of Intel, the over looked issue was that I had no real need to compile either way. I just discovered that there is no need to save my scrips bundled as applications or otherwise just to be able to run them from the menu bar. The scripts run as scripts without aid of the script editor when placed in the user library script folder. Perhaps everyone but me knew that, but I mention it here in case others like me have not discovered this.

Thanks again.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Why do Applescript applications run 60% slower than script in editor

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.