Collection v class for speed

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This is taken from here:

Code:
https://medium.com/@phunt6056/vba-and-oop-a95f9bcacaa6

This is the data structure:

1598877643641.png



I have adapted the code as follows, using combination of arrays and classes.

What I don't understand is why is Method 2 (accessing the class) faster than Method 1 (accessing the collection)?

Method 1

Code:
Option Explicit

Sub UsingOOAndArrays()

    Dim DataArray() As Variant
   
    DataArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
   
    Dim DataArrayRows As Long
   
    DataArrayRows = UBound(DataArray(), 1)
   
    Dim DataArrayCols As Long
   
    DataArrayCols = UBound(DataArray(), 2)
   
    Dim i As Long
   
    Dim House As houseobject
   
    Dim Coll As Collection
    Set Coll = New Collection
   
    For i = 2 To DataArrayRows
   
        Set House = New houseobject
       
        House.houseType = DataArray(i, 1)
        House.price = DataArray(i, 2)
        House.numberOfRooms = DataArray(i, 3)
        House.location = DataArray(i, 4)
       
        If House.getType = "Type A" And House.price > 100000 Then
           
            Coll.Add Item:=House
           
        End If
   
    Next i
   
    Dim Counter As Long
   
    Counter = Coll.Count
   
    Dim OutputArray() As Variant
   
    ReDim OutputArray(1 To Counter, 1 To DataArrayCols) As Variant
   
    Counter = 1


'***************
   
    For Each House In Coll
   
        OutputArray(Counter, 1) = Coll.Item(Counter).houseType
        OutputArray(Counter, 2) = Coll.Item(Counter).location
        OutputArray(Counter, 3) = Coll.Item(Counter).numberOfRooms
        OutputArray(Counter, 4) = Coll.Item(Counter).price
   
        Counter = Counter + 1
       
    Next House
   
'***************
   
    Sheet1.Cells(2, 16).Resize(Counter, DataArrayCols).Value = OutputArray()
   
End Sub


Method 2:

Code:
Option Explicit

Sub UsingOOAndArrays()

    Dim DataArray() As Variant
   
    DataArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
   
    Dim DataArrayRows As Long
   
    DataArrayRows = UBound(DataArray(), 1)
   
    Dim DataArrayCols As Long
   
    DataArrayCols = UBound(DataArray(), 2)
   
    Dim i As Long
   
    Dim House As houseobject
   
    Dim Coll As Collection
    Set Coll = New Collection
   
    For i = 2 To DataArrayRows
   
        Set House = New houseobject
       
        House.houseType = DataArray(i, 1)
        House.price = DataArray(i, 2)
        House.numberOfRooms = DataArray(i, 3)
        House.location = DataArray(i, 4)
       
        If House.getType = "Type A" And House.price > 100000 Then
           
            Coll.Add Item:=House
           
        End If
   
    Next i
   
    Dim Counter As Long
   
    Counter = Coll.Count
   
    Dim OutputArray() As Variant
   
    ReDim OutputArray(1 To Counter, 1 To DataArrayCols) As Variant
   
    Counter = 1


'***************
   
    For Each House In Coll

        OutputArray(Counter, 1) = House.houseType
        OutputArray(Counter, 2) = House.location
        OutputArray(Counter, 3) = House.numberOfRooms
        OutputArray(Counter, 4) = House.price

        Counter = Counter + 1

    Next House
   
'***************

   
    Sheet1.Cells(2, 16).Resize(Counter, DataArrayCols).Value = OutputArray()
   
End Sub

Thanks
 
I meant that we don't know the lower index.
Some arrays are 0 based (like what you get from Split), some are 1 based (like what you get from Range.Value), Some can vary (like what you get from Array (depending on the Option Base setting chosen). And the coder can set any LBound that they want, even a negative one).

The LBound and the UBound of an array are not fixed and things (like my generic QuickSortAnArray sub) have to take that into account unless they are carefully optimized for that specific situation.

With Collections, one knows that the LBound is always 1.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I meant that we don't know the lower index.
Some arrays are 0 based (like what you get from Split), some are 1 based (like what you get from Range.Value), Some can vary (like what you get from Array (depending on the Option Base setting chosen). And the coder can set any LBound that they want, even a negative one).

The LBound and the UBound of an array are not fixed and things (like my generic QuickSortAnArray sub) have to take that into account unless they are carefully optimized for that specific situation.

With Collections, one knows that the LBound is always 1.


Thanks for clarifying.
 
Upvote 0

Forum statistics

Threads
1,223,950
Messages
6,175,582
Members
452,653
Latest member
craigje92

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top