Peter,
Profile (time) it and see.
You will find that ArrayList is almost always more efficient. This is
because the ArrayList over allocates its internal array. ArrayList.Capacity
represents the number of elements the array list can hold (the size of its
internal array), while ArrayList.Count represents the number of elements
currently in the ArrayList (the number of times you called ArrayList.Add
without a ArrayList.Remove).
When the ArrayList.Count reaches its ArrayList.Capacity the ArrayList will
reallocate its internal array, effectively it does a "Redim Preserve", it
does this by allocating a new internal array, copying the contents of the
old internal array to the new internal array. It doubles the size of its
internal array each time it needs to reallocate it. The Capacity defaults to
16 if you do not change it via the Constructor when you create the
ArrayList. NOTE: If I plan on adding 1000 elements to the ArrayList I would
create the ArrayList with a Capacity of 1000 via the constructor.
When you use "Redim Preserve" internally VB creates a new array, then copies
all the elements from the old array to the new array. If you are Redim
Preserve with a factor of 1, then you are doing a lot of coping, especially
if you have 1000 elements!
Remember that an ArrayList's internal array is an array of Object, so if you
are storing Integers, they will be boxed going in & coming out. This may or
may not be a performance issue. You need to profile it and see. If the
boxing is causing a performance issue (proven via profiling), then what I do
is create an IntegerBuffer. Which is like an ArrayList & StringBuilder, it
maintains an over allocated internal array of integers, with Capacity &
Count properties, when I add integers to the IntegerBuffer, I increment the
Count, when the Count reaches Capacity I "Redim Preserve" the internal
array, doubling its size.
Just Remember the 80/20 rule. That is 80% of the execution time of your
program is spent in 20% of your code. I will optimize (worry about
performance, memory consumption) the 20% once that 20% has been identified &
proven to be a performance problem via profiling (CLR Profiler is one
profiling tool).
For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware...timization.pdf
Info on the CLR Profiler:
http://msdn.microsoft.com/library/de...nethowto13.asp http://msdn.microsoft.com/library/de...anagedapps.asp
NOTE: In VS.NET 2005 (aka Whidbey, due out later in 2005)
http://lab.msdn.microsoft.com/vs2005/ we will have Generics, which will give
us a generic "ArrayList" class called List(Of T) which can then use as our
"IntegerBuffer" above.
http://msdn2.microsoft.com/library/6sh2ey19.aspx
Rather then return a 2 dimensional array why not return a DataSet/DataTable?
Or a collection of Domain objects?
Hope this helps
Jay
"Peter" <pe***@mclinn.com> wrote in message
news:dc*************************@posting.google.co m...
I run into this situation all the time and I'm wondering what is the
most efficient way to handle this issue:
I'll be pulling data out of a data source and want to load the data
into an array so that I can preform complicated operations against
this data. The returned record count in these operations is always
variable.
1. I have been using an arraylist.add function to handle
non-multidemional returns but was wondering if I'm better off diming
these returns as a strString() and then redimin with presearve as the
values come in?
Example
Dim StrString() as string
dim i as integer = 0
for data looping....
redim presearve StrString(i + 1)
i +=1
next
the next question would be in the multidemin.
2. If you have a sql statement that returns multi demensional
information what is the best way to normally handle these situations.
When you do not know the record count.
Example:
sql = select Value1, Value2Subset, Value3Subset
Dim sqlHold(1, 3)
If this was more than 1000 rows.... what type of variable scope should
I use.
sqlHold(0,0) = Value1
sqlHold(0,1) = Value2Subset
sqlHold(0,1) = Value3Subset