Re: processing time & stack overflow!!!
Re: processing time & stack overflow!!!
- Subject: Re: processing time & stack overflow!!!
- From: Victor Yee <email@hidden>
- Date: Fri, 31 Aug 2001 08:03:05 -0400
On Fri, 31 Aug 2001 04:24:15 -0400, monk wrote,
>
can you explain/share to/with us what you did to
>
make the script go from an average of 12 minutes to (on my machine) 12
>
seconds
I basically did what Arthur was telling us to do:
1. use his handler ;)
2. use a reference to the reallyBigList
1. A script usually consists of two types of commands: those handled internally by AppleScript (basic math, strings, lists...) and those that are handled externally by an application or scripting addition. With the latter, AppleScript has to send a message to the receiving application or osax to process the command, and then get a message back containing the results.
In the case of "random number", that command belongs to the Standard Additions osax (osax is another term for scripting addition). Because of this, when a script invokes the command, it incurs an overhead of two messages; one to say "hey, Standard Additions, give me a random number" and the other to say "thanks, that random number is just what I needed." Normally, this overhead is barely noticed because it takes a small fraction of a second to perform, but when repeated thousands of times...
So, using Arthur's handler reduced that overhead from thousands of repetitions to just one (he's a smart guy).
2. When manipulating very large lists in AppleScript, there's a method that can be used to reduce processing overhead: a reference to someReallyBigList.
Now, this is something that I use but don't really fully understand, so I'm the wrong guy to answer. But it seems that when manipulating lists, the "reference to" construct will save considerable amounts of processing.
I would guess that it has something to do with reassigning memory. That when directly manipulating a list, e.g. setting the contents of item 1 to the contents of item 2, AppleScript has to: assign memory to hold a copy of the contents of item 2, assign the new memory address to item 1, and then release the old address.
Whereas with "reference to", it just tells item 1 "you see where item 2 's contents are in memory? Ok, that's what your contents are supposed to be as well."
But I'm just guessing. Fortunately, you can use "reference to" without needing to understand how it's _implemented_. All you need to do is understand how it _behaves_ (we'll leave the ugly stuff of impementation to the AppleScript team).
In any case, all thanks for the improved performance of the "cross cutting" script should go to Arthur (hey, he really is a smart guy! ;)
Victor