MACSCRPT Archives

February 2009

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Ryan Wilcox <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Thu, 5 Feb 2009 21:40:20 -0500
Content-Type:
text/plain
Parts/Attachments:
text/plain (75 lines)
> On Feb 5, 2009, at 5:55 PM, Shane Stanley wrote:
>
>> On 6/2/09 7:13 AM, "Ryan Wilcox" <[log in to unmask]> wrote:
>>
>>> I believed the second one would be much faster than the first as  
>>> well
>>
>> Why? The first is just modifying a string; the second is modifying  
>> a (large
>> and growing) list.

Right, but the first is calling an object into existence, with the  
value of previous variable copied[1] over, the value of a temporary  
object "a" appended to that value.. and the old variable taken care of.

The latter I imagined simple: appending to a list.

[1] whither this means copying of bytes or incrementing reference  
counts or something

On Feb 5, 2009, at 6:03 PM, Deivy Marck Petrescu wrote:
> ... modifying a string and then creating a huge list.
> But still, the second is playing with a list from the outset.
> I believe a while ago Chris Nebel put a god explanation on why lists  
> require more "work" in the Apple AppleScript list.
> If one does away with the need of manipulating a list but instead  
> uses a reference to the list then the situation changes dramatically:
>
> repeat 10000 times
> 	set end of (a reference to c) to "a"
> end repeat
> set t3 to (current date) - t2


EXCEPT the "reference to"  trick ONLY works in the (implicit or  
explicit) run hander (because that's the only place you can explicitly  
create a reference in AppleScript, which I mention in my article.  
Well, unless you make c a property - then you can use it as a reference.

If you make the c a property THEN use (a reference to c) then the code  
executes in no time at all

property c_as_refable : {}
on wayone()
	set a to "b"
	set c to {}
	--
	set t0 to (current date)
	repeat 10000 times
		set a to a & "a"
	end repeat
	set b to text items of a
	set t1 to (current date) - t0
	return t1
end wayone

on waytwo()
	set a to "b"
	set t2 to (current date)
	repeat 10000 times
		set end of (a reference to c_as_refable) to "a"
	end repeat
	set t3 to (current date) - t2
	return t3
end waytwo

set t1 to wayone()
set t2 to waytwo()
{t1, t2}

--{1, 0}

Interestinger and Interestinger,
_Ryan Wilcox

ATOM RSS1 RSS2