Discussion:
CList vs CArray
(too old to reply)
Ken->
2004-01-27 14:07:23 UTC
Permalink
I have been using CArray for everything, some of which
are actually just lists. I don't have even one CList in my code so far. I
would convert nearly half of the CArrays
to CList except for the effort involved and also not knowing
if there are any hazards with CList that make it no better than
CArray for the job.
Any comments on the advantages and disadvantages of each?
Thanks,
Ken
Scott McPhillips [MVP]
2004-01-27 14:28:07 UTC
Permalink
Post by Ken->
I have been using CArray for everything, some of which
are actually just lists. I don't have even one CList in my code so far. I
would convert nearly half of the CArrays
to CList except for the effort involved and also not knowing
if there are any hazards with CList that make it no better than
CArray for the job.
Any comments on the advantages and disadvantages of each?
Thanks,
Ken
With an array you get very fast access to every element (indexing) but
inserting or deleting an item is quite inefficient (requires mass copying).

With a list you get slow access to most elements (no indexing) but
inserting or deleting an item is efficient (no copying required).
--
Scott McPhillips [VC++ MVP]
Murrgon
2004-01-27 15:21:12 UTC
Permalink
Post by Scott McPhillips [MVP]
With an array you get very fast access to every element (indexing) but
inserting or deleting an item is quite inefficient (requires mass copying).
With a list you get slow access to most elements (no indexing) but
inserting or deleting an item is efficient (no copying required).
In general this is true. However, in certain situations things can change.
If your array/list is only storing atomic data (i.e. ints, floats, etc)
then the array is probably a better bet. The reason being is this wonderful
little thing that is entirely ignored in all algorithms analysis: the cache.

If you do an experiment with random insertions into an array and a list
(using integers, for example) you will find that up until about 8000
elements or so, the list and the array are pretty close in terms of speed.
When you get over that number of elements things change drastically.
The array becomes way faster than the list, which is somewhat counter
intuitive.

If you take a closer look at it though it becomes clear why the array is
faster. The array is stored linearly in memory, so searching through it
and even copying large parts of it are all cache friendly. The list on
the other hand is not cache friendly at all. Elements in a list are
stored all over memory so every element access can potentially cause a
cache miss. The array, even with its big memory copy, is still faster
because you waste a whole whack of time just finding the location to
do the insertion with the list. Also with a list you must store extra
data along with what you are storing in the list, namely a pointer to
the next element (at a minimum).

Storing complex structures and classes in an array or list may change
things slightly. If you consider that the time it takes to
construct/destruct your class will be the same for both the list and
the array, you can pretty much ignore that part. The part that
matters is how complex your copy constructor/operator is for your
class. This is the part that could kill the performance of the array
because, for a random insertion/deletion it needs to do a block copy.
But if you have a complex class as your elements it can't use something
simple like memcpy() to do the work.

So just be aware that there are other issues besides what the data
structure was designed for. You might not care about this either,
but a list can really fragment your memory. On a windows PC this
isn't really an issue, there is usually lots of memory. On
something like a game console or cell phone, fragmenting your
memory is a really good way to kill your application.

2 cents worth.
Murrgon
Ken->
2004-01-27 15:27:43 UTC
Permalink
Post by Scott McPhillips [MVP]
Post by Ken->
I have been using CArray for everything, some of which
are actually just lists. I don't have even one CList in my code so far.
I
Post by Scott McPhillips [MVP]
Post by Ken->
would convert nearly half of the CArrays
to CList except for the effort involved and also not knowing
if there are any hazards with CList that make it no better than
CArray for the job.
Any comments on the advantages and disadvantages of each?
Thanks,
Ken
With an array you get very fast access to every element (indexing) but
inserting or deleting an item is quite inefficient (requires mass copying).
With a list you get slow access to most elements (no indexing) but
inserting or deleting an item is efficient (no copying required).
--
Scott McPhillips [VC++ MVP]
The elements I am storing are fairly small, 4 floats and a COLORREF, so I
was wondering if the overhead of the forward and reverse pointers in a list
would use up almost as much space than the data itself. Also I don't know
how many elements I am going to have and it looked like the CList handled
that problem better than a CArray would. Now I just make the CArray way
bigger than I think it will ever get and then after I have added all of the
elements I resize it. That seems like it would be rather inefficient, but
with CArrays I don't know of another way.
I've just about talked myself into biting the bullet so to speak and convert
to CLists for a few of them. All I need is a little encouragement from
people who have used them.
Ken
Tom Serface
2004-01-27 18:15:36 UTC
Permalink
Hi Ken,
Post by Ken->
The elements I am storing are fairly small, 4 floats and a COLORREF, so I
was wondering if the overhead of the forward and reverse pointers in a list
would use up almost as much space than the data itself. Also I don't know
how many elements I am going to have and it looked like the CList handled
that problem better than a CArray would. Now I just make the CArray way
bigger than I think it will ever get and then after I have added all of the
elements I resize it. That seems like it would be rather inefficient, but
with CArrays I don't know of another way.
I've just about talked myself into biting the bullet so to speak and convert
to CLists for a few of them. All I need is a little encouragement from
people who have used them.
Ken
I think CArray would work find for this sort of thing. If you are simply
adding to the end or reassigning it shouldn't be any different from a list.
You can use the Add() and SetAt (for existing indexes) functions. The
CArray (or CObArray) will grow by the number of elements you set up in the
growby option. It really depends on the number of elements you are going to
add. If you can guestimate the size then using SetSize() will make it
faster.

Tom
Dave Harris
2004-01-29 15:06:49 UTC
Permalink
Post by Ken->
Also I don't know
how many elements I am going to have and it looked like the CList handled
that problem better than a CArray would. Now I just make the CArray way
bigger than I think it will ever get and then after I have added all of the
elements I resize it. That seems like it would be rather inefficient, but
with CArrays I don't know of another way.
Another way is to resize it continually as you go along. For example,
std::vector<> just using push_back it will grow automatically by 50% (in
v7.1) when it needs to.

Personally I'd never use CArray for new code because it lacks the features
of std::vector. For example, it doesn't have a copy-constructor or an
assignment operator - really basic stuff is missing.

-- Dave Harris

Murrgon
2004-01-27 15:59:59 UTC
Permalink
Is there a HashTable class that would have the fast retrieval
of an array as well as the fast insertion / deletion of a
list? Or a binary tree class? It looks like CMap is similar
to a hash table and may be worth a look?
A hash table can be very good at fast insertion / deletion;
it depends on the quality of your hash function. Depending
on the implementation, iteration over the elements can be
slow. CMap is MFC's idea of a hash table. If you use
it, make sure you create it with a reasonable size otherwise
it can get very inefficient.

One thing to keep in mind about hash tables, they work best
when they are at least 33%-50% bigger than the maximum number
of elements they are storing. So remember that you will
always be wasting some space when using a hash table.

Murrgon
Ken->
2004-01-27 18:39:11 UTC
Permalink
Thanks for the replies. It looks like I'm just as well off to keep the code
like I have it since I see no overwhelming testimonials for CLists.
Loading...