I have a main sub that makes use of a Client class: creates an array with 100 000 Clients and loops over the array 100 times, each time setting a different random number to each Client.
Sub start()
Application.ScreenUpdating = False
Dim j As Long
Dim clientsColl() As Client
ReDim clientsColl(1 To 100000) As Client
For j = 1 To 100000
Set clientsColl(j) = New Client
clientsColl(j).setClientName = "Client_" & j
Next
Dim clientCopy As Variant
MsgBox ("simulations start")
Dim i As Long
For i = 1 To 100
For Each clientCopy In clientsColl
clientCopy.setSimulationCount = 100
clientCopy.generateRandom
Next
Next
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox ("done")
End Sub
However, this code takes different time to run, depending whether the line clientCopy.setSimulationCount = 100 is commented out or not. If this line is commented out the code after the simulations start MsgBox takes 16 seconds to run. However, if that line is not commented out and is executed the second loop takes 2 minute 35 seconds to run.
Here's the inside of the Client class, the used Let property:
Private simulationCount As Long
Public Property Let setSimulationCount(value As Double)
simulationCount = value
End Property
Private randomNumber As Double
Public Sub generateRandom()
randomNumber = Rnd()
End Sub
So it simply puts number 100 inside each client. Why would it increase execution time nine-fold?