Optimise - Collection or array

P

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top