Bottom line is that for larger numbers of variables, storing and retrieving
the values in a collection is "much" faster than storing them in an array
(at least the way my functions are written - see below).
I wrote a function (fnMyVars2) to store and retrieve the values from an
array (I used a simple 1-to-n search of the array, since I didn't store the
keys in any particular order). If you have another idea on how to make this
routine more efficient, let me know.
Then I wrote a subroutine that would clear out all of the key values in both
functions (Collection and Array), generate a sequence of keys ("Test1" ->
"Test#") and values (double, date/time, or single character string values).
Finally, I randomly generated 500,000 keys and set a variable equal to the
value returned from each of the functions for that key (using the same
random number sequence for both techniques), and counted the number of
seconds between starting and ending each of these processes (I could have
gotten more accurate if I'd use Timer instead of Now, but didn't think about
it until just now). I ran this routine 10 times for each number of
variables, and generated the following table of variables and the ration of
seconds (Array/Collection). Keep in mind that that the maximum time to
retrieve 500,000 values from the collection was 2 seconds, and for the array
was only 8 seconds.
Variables Ratio (Array/Collection)
5 1.1
8 1.27
10 1.34
25 2.15
50 3.37
75 4.45
100 5.75
If you see any way to improve on either of these, I'd appreciate hearing
from you.
Public Function fnMyVars(key As String, _
Optional SomeValue As Variant =
Null, _
Optional Clear As Boolean = False)
As Variant
Static myVariables As New Collection
Dim intLoop As Integer
'Use in-line error processing to facilitate adding new items to the
collection
On Error Resume Next
'If SomeValue was passed, then remove the previous value and set the new
value
If Clear = True Then
For intLoop = myVariables.Count To 1 Step -1
myVariables.Remove (intLoop)
Next
ElseIf Not IsNull(SomeValue) Then
'if the key is not present, the next line will generate an error,
clear it and continue
myVariables.Remove key
myVariables.Add SomeValue, key
fnMyVars = SomeValue
Else
'Retrieve the value from the collection
fnMyVars = myVariables(key)
If IsEmpty(fnMyVars) Then fnMyVars = Null
End If
End Function
Public Function fnMyVars2(key As String, _
Optional SomeValue As Variant =
Null, _
Optional Clear As Boolean =
False) As Variant
Static myArray(100, 2) As Variant
Static aCount As Integer
Dim intLoop As Integer
If Clear = True Then
For intLoop = UBound(myArray) To LBound(myArray) Step -1
myArray(intLoop, 1) = Null
myArray(intLoop, 2) = Null
Next
aCount = 0
Exit Function
ElseIf Not IsNull(SomeValue) Then
aCount = aCount + 1
myArray(aCount, 1) = key
myArray(aCount, 2) = SomeValue
fnMyVars2 = SomeValue
Exit Function
Else
For intLoop = 1 To aCount
If myArray(intLoop, 1) = key Then
fnMyVars2 = myArray(intLoop, 2)
Exit Function
End If
Next
fnMyVars2 = Null
End If
End Function