Option Explicit
Option Private Module
'====================================================================================================
Const cyc& = 200000
'====================================================================================================
Sub TestProcess()
Dim dic As New Dictionary
Dim x, n&
dic.Add 1, 2 ' // add dic-pair Key-Value: 1-2
n = 0: Debug.Print dic.Count, dic.Keys(n) & "(" & TypeName(dic.Keys(n)) & ")", dic.Items(n) & "(" & TypeName(dic.Items(n)) & ")", vbLf ' Print: 1, 1(Integer), 2(Integer)
On Error Resume Next: dic.Add 1, 5 ' // add another Value by same Key (Error create) ' (no Debug)
Debug.Print dic.Count, Err, Err.Description: On Error GoTo 0 ' Print: 1, 457, This key is already associated with an element of this collection
dic(1) = 5 ' // Change Value (2 —> 5) by Key 1
n = 0: Debug.Print dic.Count, dic.Keys(n) & "(" & TypeName(dic.Keys(n)) & ")", dic.Items(n) & "(" & TypeName(dic.Items(n)) & ")", vbLf ' Print: 1, 1(Integer), 5(Integer)
' присваиваем переменной значение (2) по ключу 1
n = dic(1): Debug.Print dic.Count, n, TypeName(n) ' Print: 1, 5, Long // Integer —> Long (by Variable Long Type)
' то же, но с вариативной переменной
x = dic(1): Debug.Print dic.Count, x, TypeName(x) ' Print: 1, 5, Integer // True type of dic.Value
' присваиваем переменной значение по ключу 2
n = dic(2): Debug.Print dic.Count, n, TypeName(n) ' Print: 2, 0, Long // Empty —> Long (by Variable Long Type)
' то же, но с вариативной переменной
x = dic(2): Debug.Print dic.Count, x, TypeName(x) ' Print: 1, , Empty // True type of dic.Value
End Sub
'====================================================================================================
Sub TestSpeed_FillKeysOnly_StringShort_Add()
Dim dic As New Dictionary
Dim arr(), x, t!
On Error Resume Next
arr = ArrayCreate(0, True): t = Timer
For Each x In arr
dic.Add x, 0 ' 0.39
Next x
Debug.Print Timer - t, dic.Count, "StringShort_Add"
On Error GoTo 0
End Sub
'----------------------------------------------------------------------------------------------------
Sub TestSpeed_FillKeysOnly_StringShort_Assign()
Dim dic As New Dictionary
Dim arr(), x, t!
arr = ArrayCreate(0, True): t = Timer
For Each x In arr
x = dic(x) ' 0.29
Next x
Debug.Print Timer - t, dic.Count, "StringShort_Assign"
End Sub
'----------------------------------------------------------------------------------------------------
Sub TestSpeed_FillKeysOnly_StringShort_Replace()
Dim dic As New Dictionary
Dim arr(), x, t!
arr = ArrayCreate(0, True): t = Timer
For Each x In arr
dic(x) = 0 ' 0.31
Next x
Debug.Print Timer - t, dic.Count, "StringShort_Replace"
End Sub
'====================================================================================================
'====================================================================================================
Function ArrayCreate(Optional iType_0StringShort_1StringLong_2Integer_3Double&, Optional fDuples As Boolean) As Variant()
Dim aRes(), prefLong$, iType&, r&
iType = iType_0StringShort_1StringLong_2Integer_3Double: If iType < 0 Or iType > 3 Then Stop: End
ReDim aRes(1 To cyc): prefLong = String(500, "_")
For r = 1 To cyc Step 2
Select Case iType
Case 0: aRes(r) = "_" & r: If fDuples Then aRes(r + 1) = aRes(r) Else aRes(r + 1) = "_" & r + 1
Case 1: aRes(r) = prefLong & r: If fDuples Then aRes(r + 1) = aRes(r) Else aRes(r + 1) = prefLong & r + 1
Case 2: aRes(r) = r: If fDuples Then aRes(r + 1) = aRes(r) Else aRes(r + 1) = r + 1
Case 3: aRes(r) = r + 0.0001: If fDuples Then aRes(r + 1) = aRes(r) Else aRes(r + 1) = r + 1 + 0.0001
End Select
Next r
ArrayCreate = aRes
End Function
'==================================================================================================== |