On Wed, 19 Mar 2003 00:01:36 -0800, Buzz Kettles <[EMAIL PROTECTED]> wrote:

> >somewhere I read that the first (implicit conversion) is faster than 
> >the latter (string() function) ?!
> 
> that would seem unlikely to me.

Quite likely! As supported by the fact that an object reference (instance)
goes 'to string' differently in each case:

Try making this parent script:

on getRefCount me
  
  toStr1 = me.string
  refCount1 = integer(toStr1.word[3])
  
  toStr2 = string(me)
  refCount2 = integer(toStr2.word[3])
  
  toStr3 = "" & me
  refCount3 = integer(toStr3.word[3])
  
  toStr4 = ""
  put me after toStr4
  refCount4 = integer(toStr4.word[3])
  
  put [refCount1, refCount2, refCount3, refCount4]
end

Name the script 'a', then in the message window;

script("a").new().getRefCount()
-- [4, 3, 2, 2]

In other words, string() and .string seem to create additional references (and
therefore must use more cycles allocating and releasing memory) than implicit
coercion to string. 

Implicit coercion also happens with 'put after'.

.string is clearly the most 'expensive' of the three aparently equivalent
forms. It is also the most recent piece of syntactical sugar in this
discussion, so I wouldn't be surprised if it were implemented as a quick hack.

I discovered all this when experimenting with 'destroy' handlers - finding out
whether an object has more than one reference - whether it was 'safe' to cut
it loose. Of course, to do this, you have to subtract the extra references
which are created for the task, but depending on how you go 'to string' you
have to subtract a different value to get the 'true' reference count.

Also using 'put into', 'put before' and 'put after' is 'cheaper' than using an
assignment operator (with strings at least). 

str1 = str1 & str2 -- slower

put str2 after str1 -- faster

Presumably because an assignment operator requires that memory is allocated
for the right side of the operation, while the left side is still in memory.
Additionally, the 'original value' of the left side needs to get garbage
collected.

Using 'put before' or 'put after' only allocates the memory needed to
accomodate the additional string data.

With this in mind, I would also expect 'put into' to use less memory / fewer
cycles than the assignment operator (=) in those cases where a variable
already exists, especially if the variable is a string. 

Strings are passed by value, not by reference, so they are not particularly
efficient. We all know that strings can be very long (as in 'how long is a
piece of...'), so an assignment involving the concatentation of two long
strings must be quite expensive. Using 'put after' or 'put before' should
allow some of this lost efficiency to be regained.

However, I have not done any tests to confirm this. (Any takers?)

Ahh you can't keep that good ol' hypertalk syntax down.

Brennan
[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email [EMAIL 
PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for learning and helping 
with programming Lingo.  Thanks!]

Reply via email to