Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collections or Arrays 3

Status
Not open for further replies.

innmedia

Programmer
Mar 24, 2000
108
US
Hi all,

I am a point in a project where I can create arrays to hold data, or create collections. Either way I will be creating 6, and each one will need to hold up to 2000 items.

Arrays are no problem here, but I am wondering if anyone has worked with collections that contain thousands of items. Any performance issues? Any memory considerations? I believe arrays and collections are both stored the same way in RAM.

Thanks,

Ken B
 
Personally, I prefer collections. Collections can contain any type of object (strings, integers, objects). 2000 items isn't a while lot. You can save memeory if you explicitly define the dataytpe for an array, versus, say, variant. But, with collections, you don't have to worry about redim preserve.

Go with what you are comfortable with.

Scott
 
The things you need to consider from a performance point of view are:

How often will you be extending the size of the arrays using ReDim Preserve?

How much RAM will be required?

How will you be accessing the arrays?

If you can create each array of a fixed maximum size just once that is very efficient. If you are forever extending the Arrays you would be better off using a Collection.

You can work out how much RAM by taking the average size of each element and multiplying it by the number of elements. Add 20% for overheads and double the number because VB uses Unicode internally.

If you find that your code spends its time searching the arrays serially you would be better off using a Dictionary object which is a special type of collection that allows indexing by a string key and which uses hashing to provide excellent performance. The Dictionary object is part of the Scripting RunTime COM object.
 
> I believe arrays and collections are both stored the same way in RAM

Nope


>thousands of item. Any performance issues?

Adding items to collections gets slower and slower the more items you have. Basic lookups are slower than arrays BUT more flexible

You might want to consider a Dictionary (faster than a collection when adding items, particularly as the number of items increases; faster in retrieving items because of improved hashing; more flexible than collections, which in turn are more flexible than arrays)


 
Thanks All,

The 6 sets of data are strings (such as a person's name). I am initializing once, no multiple redims. Then in the program I use items scattered throughout the set(s) of data.

The number of times the collection/array is accessed is driven by a number entered into the form by a user. So, a user may enter 100, which runs a 100 iteration loop, and in the loop the collection/array is accessed to any item index (the index is determined by other factors of the application, but it will differ for each iteration).


Bear in mind, a user could enter a large value, even 1,000,000 which sets up a loop that iterates a million times. I will test this with both an array and a collection and see if the timings differ. It takes a minute or two to run the million loop anyway.

Thanks everyone!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top