• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: cost of repeats (was Re: why is applescript so slow???)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cost of repeats (was Re: why is applescript so slow???)


  • Subject: Re: cost of repeats (was Re: why is applescript so slow???)
  • From: Andreas Amann <email@hidden>
  • Date: Tue, 30 Nov 2004 21:38:18 -0800

well, sure, it's obvious that counting twice costs more. but in my original example (which takes more than a minute) i'm counting to 200 (adressbook cards) not 100000! so, i'd think the main time is spend _inside_ the reapeat not with handling the repeat loop itself.


olli

Well, the original example has some serious issues as well - I only looked at the main loop, as represented below:


tell application "Address Book"
	set refPeopleList to a reference to name of every person
	repeat with pName in refPeopleList
		set p to (first person whose name is pName)
		if (name of every group of p) does not contain "test" then
			save addressbook
		end if
	end repeat
end tell

This takes 32 on my computer (without the run log showing) - (I don't have a group "test" defined).

The problem here is that you send a lot of queries to the AB - there really is no reason to do so:

	set refPeopleList to a reference to name of every person

will have the AB look of the name for every person - this will require some time

	set p to (first person whose name is pName)

will have the AB do the same thing again - it will compare pName with the list of all names for each person - something AB has to provide again!

So, the code above is identical to

tell application "Address Book"
	repeat with p in people
		if (name of every group of p) does not contain "test" then
			save addressbook
		end if
	end repeat
end tell

which takes only 20 seconds on my computer (same conditions as above) - we just saved more then 35% in time by rewriting the code to be more efficient. NOTE: the AppleScript compiler does not have an optimizer built in like other languages/compilers do - at least not one doing the same amount of optimizations... You have to think what you ask the program to do and whether this is really needed.

Another thing really slowing down your script is the "save addressbook" after ever change - since you do all the changes automatically, it shouldn't be much of a problem if AB or something else decides to fail in the middle - you would just run the script again. Thus, I suggest saving the db only at the very end:

tell application "Address Book"
	repeat with p in people
		if (name of every group of p) does not contain "test" then
			--
		end if
	end repeat
	save addressbook
end tell

This version takes only 3 seconds to run under the same conditions.

Another few things a saw looking through your code (just random observations):

1)
on dont_copy_this(pers)
   tell application "Address Book"
       set w to words of (note of pers as string)
       return w contains "nosync"
   end tell
end dont_copy_this

Why are you taking this into words first? No need to do so, you can simply use

on dont_copy_this(pers)
tell application "Address Book" to return (note of pers as string contains "nosync")
end dont_copy_this


2)
on recreate_group(gName)
  ...

Can you possibly jump through more hoops to get the list of people in a group?

tell application "Address Book" to set delList to people of group gName

would be so much easier, better to read (and probably faster as well - I did not test this one...)

(I am not sure either whether you have to loop through each member of that list to delete them, I guess "delete delList" should work as well - I didn't try this for obvious reasons;-)

Andreas

_______________________________________________
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


  • Follow-Ups:
    • Re: cost of repeats (was Re: why is applescript so slow???)
      • From: "halloolli" <email@hidden>
    • Re: cost of repeats (was Re: why is applescript so slow???)
      • From: "halloolli" <email@hidden>
  • Prev by Date: Re: cost of repeats (was Re: why is applescript so slow???)
  • Next by Date: [ANN] Script Timer 2.2 released by Apps & More Software Design
  • Previous by thread: create new message in Mail inbox
  • Next by thread: Re: cost of repeats (was Re: why is applescript so slow???)
  • Index(es):
    • Date
    • Thread