app calls and efficiency [was Re: Best practices for ... lists of text?]
app calls and efficiency [was Re: Best practices for ... lists of text?]
- Subject: app calls and efficiency [was Re: Best practices for ... lists of text?]
- From: has <email@hidden>
- Date: Thu, 22 Dec 2005 12:48:55 +0000
John Cochrane wrote:
>While talking about best practices I have read that it is inefficient
>to have frequent separate calls to an application.
This is true, but I think you misunderstand what is meant. There are various overheads associated with application commands (for example, the time it takes the application to interpret object references), so if you can send a single, complex command instead of many simple commands to do a particular job, those overheads will be minimised. Trivial example, getting the name of every person in Address Book:
-------
set lst to {}
tell application "Address Book"
repeat with i from 1 to count people
set end of lst to name of person i
end repeat
end tell
lst
-------
tell application "Address Book"
set lst to name of every person
end tell
-------
In the second case, the entire operation is handled by a single command, 'get name of every person', instead of N+1 commands (first to count the number of people, then to get each person's name in turn). Here's a more complex case - looking up a person's name by their email address:
-------
set desiredEmail to "email@hidden"
set foundNames to {}
tell application "Address Book"
repeat with personRef in every person
repeat with emailRef in every email of person
if value of email = desiredEmail then
set end of foundNames to name of person
end if
end repeat
end repeat
end tell
foundNames
-------
set desiredEmail to "email@hidden"
tell application "Address Book"
set foundNames to name of every person where (value of every email contains desiredEmail)
end tell
-------
(There's a more detailed explanation of the above in the appscript documentation - see my site for download - but I won't reproduce it here because the list doesn't like long messages.)
Efficiency is a tricky topic. It can be hard to identify exactly where performance bottlenecks lie, especially in the AppleScript language (which lacks decent performance profiling tools) and in application scripting (where many bottlenecks are hidden deep within scriptable applications). And you really need a decent bit of CS knowledge to appreciate the issues involved and interpret what you see, which is not something many ASers will have unless they're willing and able to go off and study the literature for themselves.
Still, it's possible to pick up a few standard tricks for minimising common culprits, and there's no shortage of previous threads discussing these if you want to search the list archives.
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden