Arrays vs Lists

Wilhelm Sanke sanke at hrz.uni-kassel.de
Thu Dec 5 12:30:00 CST 2002


On Thu, 5 Dec 2002  David Vaughan <dvk at dvkconsult.com.au> wrote:


> On Thursday, December 5, 2002, at 12:32 AM, Mike McManus wrote:
>
> > I have seen a lot about arrays lately here. So I am wondering.
> >
> > 1. Why or when is an Array better than a list?
> > 2. When is a multidimensional Array better than a variable made up of
> > multiple lines with a list on each line?
> >
>
> Mike
>
> Reflecting on my practice, my approach is that I use an array when I
> wish to access elements at random or think of information as labelled
> data, whereas a list is preferred whenever I have a data blob or
> naturally deal with it in sequence. These are not exclusive, as you can
> refer to line n of a list (slow compared with an array), combine an
> array and treat as a list for a while, or sort its keys for a
> particular sequence.
>
> Since all keys in Rev arrays are just extended strings, with the
> multi-key structure being one of our imagining, I used two-dimensional
> lists (lines of items) rather than two-key arrays in the most obvious
> example in my recollection. Both lists and arrays can have delimiter
> problems (between the keys in multi-dimensional arrays) depending on
> the data.
>
> regards
> David
>
Here is an example that may illustrate different speeds and such
delimiter problems:

 To store and/or  retrieve data with a two-dimensional array takes less
than half the time needed with a two-dimensional list under the
conditions of the following example.
 Here is the storing part of a color pattern with 2760 color values used
as a basis to later manipulate the colors with specific algorithms:

array:

repeat with i = 1 to 46
    repeat with j = 1 to 60
      put the backcolor of char j of line i of fld 1 into
colorarray[i,j]
    end repeat
  end repeat

list:

put empty into colorlist
 repeat with i = 1 to 46
    repeat with j = 1 to 60
      put Space&the backcolor of char j of line i of fld 1 into  word j
of line i of colorlist
    end repeat
  end repeat

You need an delimiter with the list as the color values consist of three
items each. Using "Space&" as a necessary delimiter adds 10 % processing
time to the double processing time of the list vs. the array.

Using items instead of words is slightly faster, but then you run into
the trouble of setting and resetting itemdelimiters later when you want
to manipulate the color data, which is a bit problematic because you
have items stored in items and the disentangling takes extra time, too.

Regards,

Wilhelm Sanke




More information about the use-livecode mailing list