By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

Optimise - Collection or array

P: n/a
Pi
I'm looking at a piece of code that gets called over
100,000 times and was wondering if there would be much
performance difference between these three scenarios or
is there an alternate approach?:

== Scenario 1: ==
Dim colA as new Collection 'Collection of clsA objects

<Add 10-20 clsA objects to colA collection>

Outside Loop (100,000 times)
<code>
For Each A as clsA in colA
<code>
Next
<code>
End Outside Loop

== Scenario 2: ==
Dim arrA as Array 'Array of clsA objects

<Redimension arrA and add 10-20 clsA objects>

Outside Loop (100,000 times)
<code>
For each A as clsA in arrA
<code>
Next
<code>
End Outside Loop

== Scenario 3: ==
Dim arrA as Array 'Array of clsA objects

<Redimension arrA and add 10-20 clsA objects>

Outside Loop (100,000 times)
<code>
For i as integer = 0 to arrA.Length - 1
<code using arrA(i)>
Next
<code>
End Outside Loop
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Pi,
Have you timed them to see? (using for example System.DateTime.Now before &
after the loop and finding the difference).

Also instead of using the Microsoft.VisualBasic.Collection, have you
considered using System.Collections.ArrayList or any of the other specific
collections in the System.Collections & System.Collections.Specialized
namespaces?

As the Microsoft.VisualBasic.Collection is designed to be a very general
multi purpose collection where as System.Collections &
System.Collections.Specialized are designed to be more general specific
purpose collections.

Hope this helps
Jay

"Pi" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
I'm looking at a piece of code that gets called over
100,000 times and was wondering if there would be much
performance difference between these three scenarios or
is there an alternate approach?:

== Scenario 1: ==
Dim colA as new Collection 'Collection of clsA objects

<Add 10-20 clsA objects to colA collection>

Outside Loop (100,000 times)
<code>
For Each A as clsA in colA
<code>
Next
<code>
End Outside Loop

== Scenario 2: ==
Dim arrA as Array 'Array of clsA objects

<Redimension arrA and add 10-20 clsA objects>

Outside Loop (100,000 times)
<code>
For each A as clsA in arrA
<code>
Next
<code>
End Outside Loop

== Scenario 3: ==
Dim arrA as Array 'Array of clsA objects

<Redimension arrA and add 10-20 clsA objects>

Outside Loop (100,000 times)
<code>
For i as integer = 0 to arrA.Length - 1
<code using arrA(i)>
Next
<code>
End Outside Loop

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.