Hello Jason,
Some tips.
• When processing (large) list, define the list as property and use reference to that property in referencing to list's item. This will yield remarkable performance improvement (60 - 80 times faster as far as I can tell).
E.g. 1 Given -
--CODE1 (blooming slow)
set xx to {}
repeat with i from 1 to 1000
set end of xx to i
end repeat
set yy to {}
repeat with i from 1 to 1000
set end of yy to (xx's item i) * -1
end repeat
return yy
--END OF CODE1
--CODE1A (fast)
property xx : {}
property yy : {}
repeat with i from 1 to 1000
set end of my xx to i -- # note the usage of "my"
end repeat
repeat with i from 1 to 1000
set end of my yy to (my xx's item i) * -1 -- # idem.
end repeat
return my yy's contents
--END OF CODE1A
--CODE1B (fast and well-behaved)
main()
on main()
script o
property xx : {}
property yy : {}
repeat with i from 1 to 1000
set end of my xx to i -- # note the usage of "my" (or "o's")
end repeat
repeat with i from 1 to 1000
set end of my yy to (my xx's item i) * -1 -- # idem.
end repeat
return my yy's contents -- # return its contents, not reference
end script
tell o to run
end main
--END OF CODE1B
CODE1A and CODE1B are much faster than CODE1.
CODE1A will preserve the property values beyond each run while CODE1B won't.
In case you're employing 'reference to list defined as property' technique for performance' sake, CODE1B should be better because it won't leave unwanted dirty data behind.
*Note that you should return the contents of the reference not the reference itself, if need be, especially when you're using this technique in one of your handler which is called many times.
E.g., if you return "my yy" in lieu of "my yy's contents" and use "set aa to main()" in lieu of "main()" in CODE1B, the dynamically instantiated script object (o) cannot be released even after the handler exits because there remains an object (aa) which is bound to it in the outer script. If you return "my yy's contents", the script object (o) can be released peacefully.
• Reduce the number of costly inter-application communications.
E.g. 2 Given -
--CODE2 (pseudo-code)
set xx to {}
repeat with i from 1 to 100
set end of xx to value of cell i
end
--END OF CODE2
--CODE2A (pseudo-code)
set xx to value of cells 1 thru 100
--END OF CODE2A
CODE2A (if it's possible) should be faster than CODE2.
(CODE2A sends 1 event while CODE2 sends 100 events to Numbers.)
E.g. 3 Given -
--CODE3 (pseudo-code)
repeat with i from 1 to 100
set value of cell i to (value of cell i) * -1
end repeat
--END OF CODE3
--CODE3A (pseudo-code)
property xx : {}
set xx to value of cells 1 thru 100
repeat with i from 1 to 100
set value of cell i to (my xx's item i) * -1
end repeat
--END OF CODE3A
CODE3A (if it's possible) should be faster than CODE3.
(CODE3A sends 101 events while CODE3 sends 200 events to Numbers.)
• If Numbers has its own script menu, call the script from there. In most cases, script run via application's own script menu runs (much) faster than script run as stand-alone applet. Perhaps AppleEvent addressing mode is different between them, I'd guess.
• Inter-application comunication in applet is slower than that in Script Editor. There's a way to improve applet's performance by using 'run script' command in applet.
Something like the following template, which is designed to preserve any properties in your main code (whether or not it is terminated by error).
*Note that this template swallows any error thrown in your main code. So it's not good for developing and/or debugging your code.
--APPLET TEMPLATE
script o
script o1
-- # place your main code here
end script
try
tell o1 to run
on error --
end try
return me
end script
set o to run script o -- to preserve the properties in o1
--END OF APPLET TEMPLATE
cf.
A relevant topic: Applet speed
http://discussions.apple.com/thread.jspa?threadID=1633798&tstart=0
Hope this may be of some help,
H
Message was edited by: Hiroto (fixed typo)