On 25 jan 2011, at 22.21, Nigel Garvey wrote:
It's a fairly well understood AppleScript list phenomenon. Putting 'my' in front of a list variable name produces a reference to that variable. It's the same reference produced by 'set theDataRef to a reference to theData', only it's compiled into the code rather than being stored in a variable. Using this in references to the list's items and properties greatly speeds up access to those items and properties.
Thanks Nigel for the explanation; it was pretty obscure to me. I wasn't aware of that 'my' had that effect, only it's effect in tell blocks and script objects, and for ASObjC properties.
I did some further speed tests: But using 'my' or 'a reference to' will only work if the variable is a property or a global, i.e it won't work for a local variable in a handler. This can be solved, however, by creating a temporary (local) script object inside the handler, which makes it possible to set a (local) property in the script object, thereby making it possible to use 'my' for all cases of appending to a list. Tests reveal that already with a 100-element list there is a significant gain, for 1000 elements the gain is huge, for 10000 elements it won't work without 'my'. There is virtually no extra cost associated with this "embedded" script object, so it is usable even for a 10-element list, although there is no gain to be had for such small lists compared with a solution that does not use 'my' or 'a reference to'.
After the original measurement it occurred to me that I did run the computer without battery, which strangely makes it slow down a lot. With battery installed, it ran the script in about 6.6 seconds rather than 12.5 seconds I first mentioned.
(The reason I sometimes run it without battery when using external monitor is to save the battery somewhat (reduced aging/damaging) and it makes the fan run slow, making it quiet.)
--heb |