Re: Deleting last item of a List
Re: Deleting last item of a List
- Subject: Re: Deleting last item of a List
- From: has <email@hidden>
- Date: Sat, 20 Nov 2004 15:03:25 +0000
Hanaan Rosenthal wrote:
The reason why using 'set end of' executes faster is because it creates
a list with a slightly different use of memory.
The two ways to create a list are:
1. set theList to theList & newItem
2. set end of theList to newItem
Unless the item you are adding is a list by itself, the two options
will create a seemingly identical list.
Incorrect. Method 1 creates a _new_ list object whereas Method 2
modifies an _existing_ one. (BTW, it's important folk appreciate the
difference here, as modifying a mutable object without realising
you're doing so is a common source of hard-to-trace errors for
unsuspecting novices.)
Method 2 will create a list faster, but it will be spread out in
memory, and therefor will be slower to work with.
Nonsense.
Years ago, you could actually decide what type of list you want to
create by using square brackets, which still works, but I am not sure
what the implications are.
set myList to [1,2,3,4]
That's a linked list, which were superceded in AS 1.1(?) by vector
lists, denoted by {}. AppleScript's linked lists are deprecated and
will eventually be removed, so don't use them. If you really want to
use linked lists in AppleScript, you can implement your own easily
enough, e.g. the Types library on AppleMods uses linked lists to
implement its Stack and Queue objects. However, most ASers don't know
(or need to know!) the difference between linked and vector lists so
should just stick to using AS's standard vector lists and not worry
about any of this stuff.
...
Shane Stanley wrote:
>So do the lists differ, as I said, in the way the are handled in
memory, or not?
Not in the way you suggested. The difference is that one version creates
a new list, while the other merely modifies an existing list -- the
efficiency comes from this fact, not anything to do with being spread out
in memory.
Specifically, it's due to the algorithm AppleScript uses to grow a
list when a value is appended to it.
In non-technical terms: when creating a new list in memory,
AppleScript allocates more RAM than is actually needed to hold the
list data that's initially provided. This extra space provides a bit
of 'growing room' that makes appending to a list (Method 2) extremely
efficient since inserting a value into that unused space takes just a
couple of very fast, simple operations. Of course, once that extra
space is completely filled then to grow the list any further
AppleScript has to allocate a fresh patch of RAM and move all the
data over to that - a much more expensive operation - but unlike
using concatenation (Method 1) this doesn't occur every time your
script appends a new value to the list so it works out cheaper
overall.
Mind, this is all just internal implementation details that end-users
ideally shouldn't have to concern themselves with. They should simply
use whichever solution - concatenating or appending - is most
appropriate to their needs.
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden