Applescript vs. Visual Basic speed

P

Possum Stu

Suppose you are only scripting Microsoft Office 2004 applications for
the Macintosh (OSX). If you were to write the "same script" in Visual
Basic and in Applescript -- that is, follow the same logic and apply
similar commands and perform the same loops -- would one execute faster
than the other? If so, which is faster? Substantially so?

I'd appreciate comments from anyone who ever tried to "speed things up"
by switching from one language to the other. What was your experience?
Thanks.
 
P

Paul Berkowitz

Suppose you are only scripting Microsoft Office 2004 applications for
the Macintosh (OSX). If you were to write the "same script" in Visual
Basic and in Applescript -- that is, follow the same logic and apply
similar commands and perform the same loops -- would one execute faster
than the other? If so, which is faster? Substantially so?

I'd appreciate comments from anyone who ever tried to "speed things up"
by switching from one language to the other. What was your experience?


It should not make much difference, but might be a little faster as a VBA
macro because Word itself is running the process. AppleScript is running
from outside - either an AppleScript application ("applet") which has to
launch, or a script running from the system script menu (meaning
"SystemUIServer" is running it.) There's no built-in script menu in Word,
Excel, etc/.If you're asking whether an AppleScript using native 2004
commands or 'do Visual Basic' would be faster, I don't know and I don't
suppose the difference would be very great, but I'd guess that the native
AppleScript would be a little faster. The only time you'd notice any
difference anyway would be in a batch process that repeated the same
functions over and over again - say on 500 or 1000 documents. If you ever do
any tests on this - which is the only way to know for sure - please report
back and let us know - with timings.

Office 2004's AppleScript and VBA each access the same object model at a
deeper level - something called "OLE Automation". Native 2004 AppleScript
does not route itself through VBA first - although 'do Visual Basic' does.
There are some aspects of OLE Automation that have had to be "translated" a
bit for AppleScript (some aspects of 'range', for example) - that might
involve some overhead. On the other hand there are some aspects of VBA which
would have had to be "translated" a bit for the Mac OS which native
AppleScript may be able to access directly. I suspect that the biggest
difference will just be that Word itself (and Excel. PPT) run VBA macros.

Anyway, do let us know if you find any appreciable differences.


--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
B

Bob Greenblatt

Out of curiosity, I tested this.

This VBA code:

Sub TestTime()
Dim TNow As Date
TNow = Now
For i = 1 To 1000
With ThisWorkbook.Sheets(1)
.Range("a1") = .Range("a1") + 1
End With
Next
MsgBox Format(Now - TNow, "hh:mm:ss")
End Sub


Ran about 5 times faster than this applescript:

set tNow to current date
tell application "Microsoft Excel"
repeat 1000 times
set value of cell "$a$1" to (value of cell "$a$1") + 1
end repeat
end tell

display dialog (current date) - tNow
 
J

JE McGimpsey

Bob Greenblatt said:
Out of curiosity, I tested this.

This VBA code:

<snip>

Ran about 5 times faster than this applescript:

<snip>


For me the VBA ran 30 times faster. Averaging 5 trials each:

VBA: 1.2 seconds (plus or minus 0.1)
AppleScript: 36 seconds avg. (plus or minus 2)
AS Application: 43 seconds avg. (plus or minus 2)

It surprised me that saving the AppleScript as an application and
running it as an app was so much slower.
 
P

Paul Berkowitz

For me the VBA ran 30 times faster. Averaging 5 trials each:

VBA: 1.2 seconds (plus or minus 0.1)
AppleScript: 36 seconds avg. (plus or minus 2)
AS Application: 43 seconds avg. (plus or minus 2)

It surprised me that saving the AppleScript as an application and
running it as an app was so much slower.

That's pretty consistent with all AppleScripts. The biggest part of that
difference is the launch time (bouncing in the dock as it gears itself up),
but there's a bit of overhead also compared to running from a script editor
or the system script menu. Apps which have their own script menus, like
Entourage, run _much_ faster from those, but not 30 times faster , more like
4 or 5 times faster - due to the fact that the app itself is running the
script and no AppleEvent queuing up at the AppleEvent Manager is needed, and
no inter-application messages are sent: it's all done by the 'current
application'. So although much of the faster performance from a VBA macro
will be due to the similar advantage that it's being run by Word itself,
some of it it must be to other performance factors of VBA being more
"native" hooking into OLE Automation.

I pleaded many times for script menus for Word, Excel and PPT, but these
were dropped late in the day (they were going to be added "at the end" but
never were). If you guys would like to add your voices to requesting them,
that would be a big help. Performance data like you're providing now would
help a lot. Most of the performance hit in AppleScript is due to the scripts
being run from outside the app.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top