PC Review


Reply
Thread Tools Rate Thread

Array vs arraylist variable scope

 
 
Peter
Guest
Posts: n/a
 
      19th Oct 2004
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
 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      19th Oct 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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



 
Reply With Quote
 
Larry Serflaten
Guest
Posts: n/a
 
      19th Oct 2004

"Peter" <(E-Mail Removed)> wrote

> 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.


Which is why the ArrayList would work well....


> 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?


Stay with the ArrayList, that is what it was designed for.



> 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.


Use a Structure to hold the data, and another ArrayList to hold the
structures.

A strongly typed collection would probably be more appropreate, but
the ArrayList is ready for use out of the box....

LFS

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      19th Oct 2004
Peter,

It is very simple, when you have a static array from which you know the rows
will never change. Than a fixed array. When not, than any array or
collection that implements IList or ICollection where my favorite when it is
simple the arraylist.

http://msdn.microsoft.com/library/de...ClassTopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps?

Cor




 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      19th Oct 2004
Doh!
> You will find that ArrayList is almost always more efficient.

I really should have said "performs faster", as the boxing of value types
may make it more inefficient...

Jay

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable scope in nested classes - can't get to my variable JohnR Microsoft VB .NET 5 12th Mar 2005 04:35 PM
Variable SCOPE VB Joshua Microsoft ASP .NET 2 3rd Sep 2004 01:03 PM
Variable scope TonyM Microsoft Excel Programming 5 24th Apr 2004 01:02 PM
ADSI returning groups in Global scope and Domain local scope instead of Universal scope Maziar Aflatoun Microsoft C# .NET 1 21st Apr 2004 04:08 PM
Variable Scope, please help al Microsoft VB .NET 3 17th Dec 2003 12:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:40 PM.