array vs list

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

array vs list

Post by Acki »

Hi,
I'm wondering what the benefits of using list<t> and array<t> are !?!?!
Well, the array has more functions to work with...
Are there any other differences/benefits of using the one ore the other ???
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Speed of access, the ways in which you want to access it... Im sure there's plenty of websites out there with some comparisons going on. Can't really remember the details from Uni :lol:
Image Image Image
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Ahh, I thought so, a list is faster than an array... ;)

thx
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Depends what you're doing. If you want to access element near the end of your container then the array is going to be quicker than the list as with the list you have to iterate through all the elements to get to the end, whereas with the array you have instant access.
Image Image Image
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

In most cases array is faster or equally fast, but it really depends on your overall container usage. List is faster when you delete/insert items in the middle.

array has the huge advantage, in that you have random access to the items within. While in a list you can only hop from item to item. This is for instance why array has sort and list hasn't. Sorting a list is inefficient (bubble sort works ok with it, but else...)

While iterating over all items has the same complexity for both, array is more cache friendly.

An array stores its items in a continuous block of memory. This means, if you delete an item in the middle, all items behind it have to be moved accordingly. Same for deletion. Also if the current size gets larger than the size already allocated, then the array has to be reallocated. A new block is allocated and all data moved there.
Such operations are expensive, it is thus important to guess a good size and set it up-front.
varnie
Posts: 31
Joined: Wed Jul 19, 2006 8:27 pm
Location: Russia, Ural

Post by varnie »

fully agree with JP.
as for 'addon' i would quote a bit from a good "Addison Wesley: Software Engineering and Computer Games" book, which i've read not so long ago:
Should we use a list or an array for our collection of critter pointers? Well, iterating through an array is faster than iterating through a list, but deleting objects is faster with a list than with an array. The cost of deleting something from the middle of an array is noticeable because then all of the higher-indexed array members need to be moved down one position in the array. On the whole, we expect more of our computation time to involve iterations than object deletions, so we'll use an array.
hope, it helps!
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The cost of deleting something from the middle of an array is noticeable because then all of the higher-indexed array members need to be moved down one position in the array
This statement is not necessarily true. You can _usually_ swap the last item in an array with the one that you want to remove, and then remove the last item in the array. As long as the list doesn't have to be sorted...
as with the list you have to iterate through all the elements to get to the end, whereas with the array you have instant access
This is only the case with a singly-linked list. Most lists are implemented as doubly-linked lists [with pointers to the first and last node], so access to the front and back of the list is constant time. If it is a singly-linked list [with just a pointer to the first node] then access to the front of the list is constant time and access to the tail is linear time.

Travis
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Moreover, vector, list, queue, map, etc, are usually only abstract data type concepts for access interfaces to containers. Most libraries (among them the STL) make some assumptions about runtime (usually average times) because otherwise some concepts would not make sense. But no one would prohibit a vector which uses a linked list beneath to speed up intermediate removal of elements.
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

hybrid wrote:But no one would prohibit a vector which uses a linked list beneath to speed up intermediate removal of elements.
Unless you count common sense too. ;)

Most efficient way to implement vector with its traits is a continuous block of memory. Eventhough, the standard doesn't say it explicitly, it is implicitly assumed by STL users, that &myvector[0] returns the address to the first item of the container and all others follow behind. If this wasn't the case, you couldn't hand over a vector to a c function expecting a pointer to a block of memory and a count.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Indeed the STL standard now includes such a requirement. But I think Java does it very differently :wink:
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

Not so differently actually. java.util.Vector and java.util.ArrayList also use a continuous block of memory for their storage.
Post Reply