Collection vs. Array In VB 
Author Message
 Collection vs. Array In VB

Hi guys,
If i want to store a simple string like:
"ID_MAIN"
"ID_SUBMAIN"
"ID_FUNC"
"ID_SUBFUNC"

My  questions is:
1)     Which way is better or most efficient to store such a string?
        Collection or Array?

2)    Which one is faster when I access those string in both collection and
array.

3)    And lastly which one will take up the most memory?

Thanks.



Mon, 24 Nov 2003 11:50:56 GMT  
 Collection vs. Array In VB
An array wins hands down on all items.  A Collection gives you powerful
functionality at a price.  If you need to iterate through items using
for..each, or find a particular item based on a key, then you MAY want to
use a Collection.  If you are concerned about performance, you almost always
can beat a Collection with an array and some code.

--

Brainbench MVP for Visual Basic
http://www.brainbench.com


Quote:
> Hi guys,
> If i want to store a simple string like:
> "ID_MAIN"
> "ID_SUBMAIN"
> "ID_FUNC"
> "ID_SUBFUNC"

> My  questions is:
> 1)     Which way is better or most efficient to store such a string?
>         Collection or Array?

> 2)    Which one is faster when I access those string in both collection
and
> array.

> 3)    And lastly which one will take up the most memory?

> Thanks.



Mon, 24 Nov 2003 12:37:57 GMT  
 Collection vs. Array In VB

Quote:
> An array wins hands down on all items.  A Collection gives you powerful
> functionality at a price.  If you need to iterate through items using
> for..each, or find a particular item based on a key, then you MAY want to
> use a Collection.  If you are concerned about performance, you almost
always
> can beat a Collection with an array and some code.

All of the above is correct, but I have some slight footnotes:
1. If you use your collection to store not only values but also keys, then
you have a nicely encapsulated class that does the searching for you
(allthough I would use a Dictionary for that).  Since collections use
hashing they beat an array on that (because you would either have to hash
yourself or do a binary search which is slower)
2. A collection is easily passed around.  An array should be either passed
ByRef or made available globally, which is not best practice.
3. If you actually need a remove - either key or index-based- then the
collection wins too.  Certainly in simplicity.
So its not which is fastest.  It depends on what you wnat to do.  Take a
mountain bike to bike a mountain but take a car to travel a long road.
Quote:

> --

> Brainbench MVP for Visual Basic
> http://www.brainbench.com



> > Hi guys,
> > If i want to store a simple string like:
> > "ID_MAIN"
> > "ID_SUBMAIN"
> > "ID_FUNC"
> > "ID_SUBFUNC"

> > My  questions is:
> > 1)     Which way is better or most efficient to store such a string?
> >         Collection or Array?

> > 2)    Which one is faster when I access those string in both collection
> and
> > array.

> > 3)    And lastly which one will take up the most memory?

> > Thanks.



Mon, 24 Nov 2003 15:53:47 GMT  
 Collection vs. Array In VB
Also you could use an Enum, all based on what you are
going to use it for.



Quote:


> > An array wins hands down on all items.  A Collection gives you powerful
> > functionality at a price.  If you need to iterate through items using
> > for..each, or find a particular item based on a key, then you MAY want
to
> > use a Collection.  If you are concerned about performance, you almost
> always
> > can beat a Collection with an array and some code.
> All of the above is correct, but I have some slight footnotes:
> 1. If you use your collection to store not only values but also keys, then
> you have a nicely encapsulated class that does the searching for you
> (allthough I would use a Dictionary for that).  Since collections use
> hashing they beat an array on that (because you would either have to hash
> yourself or do a binary search which is slower)
> 2. A collection is easily passed around.  An array should be either passed
> ByRef or made available globally, which is not best practice.
> 3. If you actually need a remove - either key or index-based- then the
> collection wins too.  Certainly in simplicity.
> So its not which is fastest.  It depends on what you wnat to do.  Take a
> mountain bike to bike a mountain but take a car to travel a long road.

> > --

> > Brainbench MVP for Visual Basic
> > http://www.brainbench.com



> > > Hi guys,
> > > If i want to store a simple string like:
> > > "ID_MAIN"
> > > "ID_SUBMAIN"
> > > "ID_FUNC"
> > > "ID_SUBFUNC"

> > > My  questions is:
> > > 1)     Which way is better or most efficient to store such a string?
> > >         Collection or Array?

> > > 2)    Which one is faster when I access those string in both
collection
> > and
> > > array.

> > > 3)    And lastly which one will take up the most memory?

> > > Thanks.



Mon, 24 Nov 2003 17:38:48 GMT  
 Collection vs. Array In VB
Hi Eric,

Slight correction.  You don't need to use a collection to iterate through items
using For..Each.  You can also do it with arrays.  For example:

    Dim ab(1 To 10) As Byte
    Dim b As Variant
    Dim i As Integer
    For i = 1 To 10
        ab(i) = i
    Next
    For Each b In ab
        Debug.Print b
    Next

John.........

Quote:

> An array wins hands down on all items.  A Collection gives you powerful
> functionality at a price.  If you need to iterate through items using
> for..each, or find a particular item based on a key, then you MAY want to
> use a Collection.  If you are concerned about performance, you almost always
> can beat a Collection with an array and some code.

> --

> Brainbench MVP for Visual Basic
> http://www.brainbench.com



> > Hi guys,
> > If i want to store a simple string like:
> > "ID_MAIN"
> > "ID_SUBMAIN"
> > "ID_FUNC"
> > "ID_SUBFUNC"

> > My  questions is:
> > 1)     Which way is better or most efficient to store such a string?
> >         Collection or Array?

> > 2)    Which one is faster when I access those string in both collection
> and
> > array.

> > 3)    And lastly which one will take up the most memory?

> > Thanks.



Mon, 24 Nov 2003 20:42:46 GMT  
 Collection vs. Array In VB
This isn't for each ... next.  This is indexing into the array, which you
can also do with collections.  Yes, collections are slower, but they are
also dynamic, allowing items to be added and removed without having to
explicitely rediminsion.
--
Mike Ober.


Quote:
> Hi Eric,

> Slight correction.  You don't need to use a collection to iterate through
items
> using For..Each.  You can also do it with arrays.  For example:

>     Dim ab(1 To 10) As Byte
>     Dim b As Variant
>     Dim i As Integer
>     For i = 1 To 10
>         ab(i) = i
>     Next
>     For Each b In ab
>         Debug.Print b
>     Next

> John.........


> > An array wins hands down on all items.  A Collection gives you powerful
> > functionality at a price.  If you need to iterate through items using
> > for..each, or find a particular item based on a key, then you MAY want
to
> > use a Collection.  If you are concerned about performance, you almost
always
> > can beat a Collection with an array and some code.

> > --

> > Brainbench MVP for Visual Basic
> > http://www.brainbench.com



> > > Hi guys,
> > > If i want to store a simple string like:
> > > "ID_MAIN"
> > > "ID_SUBMAIN"
> > > "ID_FUNC"
> > > "ID_SUBFUNC"

> > > My  questions is:
> > > 1)     Which way is better or most efficient to store such a string?
> > >         Collection or Array?

> > > 2)    Which one is faster when I access those string in both
collection
> > and
> > > array.

> > > 3)    And lastly which one will take up the most memory?

> > > Thanks.



Mon, 24 Nov 2003 21:58:10 GMT  
 Collection vs. Array In VB
That's not to say that the collection doesn't redim an array....it does, in
it's own way.

Any binary-search algorithm can be used to make a custom EARLY BOUND
collection class that kicks the shit out of the LATE BOUND vb.Collection
class.

Don't get me worng, if you got less than..say...I duno, 50 items to add to
the collection, or your app doesn't use it all-the-time...just use the
generic collection, and save the headache.

Otherwise use an array, or an custom array-wrapper class. (I've gotten so
accustomed to quickly writing custom array wrappers that I haven't used the
generic collection class in YEARS.)

I must add....IMHO.... stuff like.....

Dim FL as file, FO as folder

for each FL in FO.Files
    ....
    ....
next

Drives me nuts! I can't stand it!



Quote:
> This isn't for each ... next.  This is indexing into the array, which you
> can also do with collections.  Yes, collections are slower, but they are
> also dynamic, allowing items to be added and removed without having to
> explicitely rediminsion.
> --
> Mike Ober.



> > Hi Eric,

> > Slight correction.  You don't need to use a collection to iterate
through
> items
> > using For..Each.  You can also do it with arrays.  For example:

> >     Dim ab(1 To 10) As Byte
> >     Dim b As Variant
> >     Dim i As Integer
> >     For i = 1 To 10
> >         ab(i) = i
> >     Next
> >     For Each b In ab
> >         Debug.Print b
> >     Next

> > John.........


> > > An array wins hands down on all items.  A Collection gives you
powerful
> > > functionality at a price.  If you need to iterate through items using
> > > for..each, or find a particular item based on a key, then you MAY want
> to
> > > use a Collection.  If you are concerned about performance, you almost
> always
> > > can beat a Collection with an array and some code.

> > > --

> > > Brainbench MVP for Visual Basic
> > > http://www.brainbench.com



> > > > Hi guys,
> > > > If i want to store a simple string like:
> > > > "ID_MAIN"
> > > > "ID_SUBMAIN"
> > > > "ID_FUNC"
> > > > "ID_SUBFUNC"

> > > > My  questions is:
> > > > 1)     Which way is better or most efficient to store such a string?
> > > >         Collection or Array?

> > > > 2)    Which one is faster when I access those string in both
> collection
> > > and
> > > > array.

> > > > 3)    And lastly which one will take up the most memory?

> > > > Thanks.



Tue, 25 Nov 2003 02:41:11 GMT  
 Collection vs. Array In VB
You can always get down and dirty when speed really counts. Otherwise, I
would just use the collection.

Ask yourself, Does it really matter if X takes 2 milliseconds more than Y?
Will the user would care, or notice?

Measurable vs. noticeable is like a scale ...

+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
Monte Hansen
http://KillerVB.com
+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+


Quote:
> Hi guys,
> If i want to store a simple string like:
> "ID_MAIN"
> "ID_SUBMAIN"
> "ID_FUNC"
> "ID_SUBFUNC"

> My  questions is:
> 1)     Which way is better or most efficient to store such a string?
>         Collection or Array?

> 2)    Which one is faster when I access those string in both collection
and
> array.

> 3)    And lastly which one will take up the most memory?

> Thanks.



Tue, 25 Nov 2003 12:28:17 GMT  
 Collection vs. Array In VB
If the strings are constants, use resources...
Quote:


> > > > Hi guys,
> > > > If i want to store a simple string like:
> > > > "ID_MAIN"
> > > > "ID_SUBMAIN"



Tue, 25 Nov 2003 19:28:47 GMT  
 Collection vs. Array In VB
If a Collection is implemented as a Hashtable, then searching for a value (by
key) in a Collection will vastly outperform any binary search you can write.
If you dont believe me, insert 100,000 keys/values in a Collection and a custom
binary search array. However, before you do the insertion, check to see that the
value is NOT already in the Array/Collection.
The complexity for the Collection (Hashtable) is O(N) but O(NlogN) for the
Array.

--Olu E.

Quote:

> That's not to say that the collection doesn't redim an array....it does, in
> it's own way.

> Any binary-search algorithm can be used to make a custom EARLY BOUND
> collection class that kicks the shit out of the LATE BOUND vb.Collection
> class.

> Don't get me worng, if you got less than..say...I duno, 50 items to add to
> the collection, or your app doesn't use it all-the-time...just use the
> generic collection, and save the headache.

> Otherwise use an array, or an custom array-wrapper class. (I've gotten so
> accustomed to quickly writing custom array wrappers that I haven't used the
> generic collection class in YEARS.)

> I must add....IMHO.... stuff like.....

> Dim FL as file, FO as folder

> for each FL in FO.Files
>     ....
>     ....
> next

> Drives me nuts! I can't stand it!



> > This isn't for each ... next.  This is indexing into the array, which you
> > can also do with collections.  Yes, collections are slower, but they are
> > also dynamic, allowing items to be added and removed without having to
> > explicitely rediminsion.
> > --
> > Mike Ober.



> > > Hi Eric,

> > > Slight correction.  You don't need to use a collection to iterate
> through
> > items
> > > using For..Each.  You can also do it with arrays.  For example:

> > >     Dim ab(1 To 10) As Byte
> > >     Dim b As Variant
> > >     Dim i As Integer
> > >     For i = 1 To 10
> > >         ab(i) = i
> > >     Next
> > >     For Each b In ab
> > >         Debug.Print b
> > >     Next

> > > John.........


> > > > An array wins hands down on all items.  A Collection gives you
> powerful
> > > > functionality at a price.  If you need to iterate through items using
> > > > for..each, or find a particular item based on a key, then you MAY want
> > to
> > > > use a Collection.  If you are concerned about performance, you almost
> > always
> > > > can beat a Collection with an array and some code.

> > > > --

> > > > Brainbench MVP for Visual Basic
> > > > http://www.brainbench.com



> > > > > Hi guys,
> > > > > If i want to store a simple string like:
> > > > > "ID_MAIN"
> > > > > "ID_SUBMAIN"
> > > > > "ID_FUNC"
> > > > > "ID_SUBFUNC"

> > > > > My  questions is:
> > > > > 1)     Which way is better or most efficient to store such a string?
> > > > >         Collection or Array?

> > > > > 2)    Which one is faster when I access those string in both
> > collection
> > > > and
> > > > > array.

> > > > > 3)    And lastly which one will take up the most memory?

> > > > > Thanks.

--
"In the sea there are riches beyond compare ... but if it's safety you
seek, your place is on the shore."

--Sufi poet, 15th C.



Wed, 24 Dec 2003 19:05:31 GMT  
 Collection vs. Array In VB
Isn't the BEST-CASE (excuse the caps, in lack of italics) O of a hashtable
N?  Certainly, it's usually the average-case too, but it can sometimes be
greater (i.e. when space starts to run low in the array, usually around 80%
if I remember correctly).

Just a little clarification (or learning experience if I'm wrong).


Quote:
> If a Collection is implemented as a Hashtable, then searching for a value
(by
> key) in a Collection will vastly outperform any binary search you can
write.
> If you dont believe me, insert 100,000 keys/values in a Collection and a
custom
> binary search array. However, before you do the insertion, check to see
that the
> value is NOT already in the Array/Collection.
> The complexity for the Collection (Hashtable) is O(N) but O(NlogN) for the
> Array.

> --Olu E.


> > That's not to say that the collection doesn't redim an array....it does,
in
> > it's own way.

> > Any binary-search algorithm can be used to make a custom EARLY BOUND
> > collection class that kicks the shit out of the LATE BOUND vb.Collection
> > class.

> > Don't get me worng, if you got less than..say...I duno, 50 items to add
to
> > the collection, or your app doesn't use it all-the-time...just use the
> > generic collection, and save the headache.

> > Otherwise use an array, or an custom array-wrapper class. (I've gotten
so
> > accustomed to quickly writing custom array wrappers that I haven't used
the
> > generic collection class in YEARS.)

> > I must add....IMHO.... stuff like.....

> > Dim FL as file, FO as folder

> > for each FL in FO.Files
> >     ....
> >     ....
> > next

> > Drives me nuts! I can't stand it!



> > > This isn't for each ... next.  This is indexing into the array, which
you
> > > can also do with collections.  Yes, collections are slower, but they
are
> > > also dynamic, allowing items to be added and removed without having to
> > > explicitely rediminsion.
> > > --
> > > Mike Ober.



> > > > Hi Eric,

> > > > Slight correction.  You don't need to use a collection to iterate
> > through
> > > items
> > > > using For..Each.  You can also do it with arrays.  For example:

> > > >     Dim ab(1 To 10) As Byte
> > > >     Dim b As Variant
> > > >     Dim i As Integer
> > > >     For i = 1 To 10
> > > >         ab(i) = i
> > > >     Next
> > > >     For Each b In ab
> > > >         Debug.Print b
> > > >     Next

> > > > John.........


> > > > > An array wins hands down on all items.  A Collection gives you
> > powerful
> > > > > functionality at a price.  If you need to iterate through items
using
> > > > > for..each, or find a particular item based on a key, then you MAY
want
> > > to
> > > > > use a Collection.  If you are concerned about performance, you
almost
> > > always
> > > > > can beat a Collection with an array and some code.

> > > > > --

> > > > > Brainbench MVP for Visual Basic
> > > > > http://www.brainbench.com



> > > > > > Hi guys,
> > > > > > If i want to store a simple string like:
> > > > > > "ID_MAIN"
> > > > > > "ID_SUBMAIN"
> > > > > > "ID_FUNC"
> > > > > > "ID_SUBFUNC"

> > > > > > My  questions is:
> > > > > > 1)     Which way is better or most efficient to store such a
string?
> > > > > >         Collection or Array?

> > > > > > 2)    Which one is faster when I access those string in both
> > > collection
> > > > > and
> > > > > > array.

> > > > > > 3)    And lastly which one will take up the most memory?

> > > > > > Thanks.

> --
> "In the sea there are riches beyond compare ... but if it's safety you
> seek, your place is on the shore."

> --Sufi poet, 15th C.



Fri, 09 Jan 2004 11:22:28 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Collection vs. Array In VB

2. Collection vs. Array In VB

3. Collection vs. Array In VB

4. Collection vs. Array In VB

5. ? Collections VS Arrays VS Types, etc ?

6. ? Collections VS Arrays VS Types, etc ?

7. Sorting user-defined objects - Collection vs. array

8. Collection vs Array

9. Control collection vs Control Array

10. Class collections vs Arrays

11. Collections VS arrays

12. Performance Collection vs. String Array

 

 
Powered by phpBB® Forum Software