' este proyecto presenta una comparativa con 3 métodos diferentes para
' verificar si un array está INICIALIZADO (O NO), y proceder en cada caso
' a redimensionarlo o iniciarlo.
' metodo 2 emplea este registro
Private Type miRegistro
suArray() As Integer
contador As Integer
End Type
Dim suArray() As Integer ' emplean métodos 0 y 1 método ON ERROR
Dim miArray ' lo emplea método 1 método VARIANT
Dim tuArray As miRegistro ' emplea el método 2 método REGISTRO
' comCalcular es un botón, txttiempo y TxtByte son 2 cajas de texto, frametodo es un Frame que contiene a 3 controles option que representan el método elegido
' hscrRep es un scroll horizontal para modificar el número de repeticiones, finalmente hay un label que indica el valor del scroll...
Private Sub ComCalcular_Click()
Dim t As Single ' para el cálculo del tiempo empleado
Dim tamByte As Long ' para el cálculo de la memoria que ocupa
Me.TxtTiempo.Text = 0
Me.TxtByte.Text = 0
DoEvents
If FraMetodo.Tag = 0 Then ' Método ON ERROR
t = Timer
For j = 0 To 10
For k = 1 To HScrRep.Value
If limiteA(suArray) <> -1 Then
ReDim Preserve suArray(0 To UBound(suArray))
suArray(UBound(suArray)) = k
Else
ReDim suArray(0 To 0)
suArray(0) = 0
End If
Next
Erase suArray
Next j
TxtTiempo.Text = Timer - t
TxtByte.Text = (tamByte + HScrRep.Value) * Len(suArray(0))
ElseIf FraMetodo.Tag = 1 Then ' Método VARIANT
t = Timer
For j = 0 To 10
For k = 1 To HScrRep.Value
'MsgBox VarType(miArray)
If limiteB(miArray) <> -1 Then
ReDim Preserve suArray(0 To UBound(suArray))
suArray(UBound(suArray)) = k
Else
ReDim suArray(0 To 0)
suArray(0) = 0
End If
miArray = suArray
Next
Erase suArray
Set miArray = Nothing
Next j
TxtTiempo.Text = Timer - t
TxtByte.Text = (tamByte + HScrRep.Value) * Len(suArray(0))
Else ' Método REGISTRO
t = Timer
For j = 0 To 10
tuArray.contador = -1
For k = 1 To HScrRep.Value
If tuArray.contador <> -1 Then
tuArray.contador = tuArray.contador + 1
ReDim Preserve tuArray.suArray(0 To tuArray.contador)
Else
tuArray.contador = 0
ReDim tuArray.suArray(0 To 0)
End If
tuArray.suArray(tuArray.contador) = k
Next
Erase tuArray.suArray
Next
TxtTiempo.Text = Timer - t
TxtByte.Text = (tamByte + HScrRep.Value) * Len(tuArray)
End If
Erase suArray, tuArray.suArray
End Sub
' sólo procesa el tipo integer, para otro tipo debe proveerse otra función casi idéntica
Public Function limiteA(matriz() As Integer) As Integer
On Local Error GoTo limiteError
limiteA = UBound(matriz)
Exit Function
limiteError:
limiteA = -1
End Function
' procesa tanto matrices de enteros como de cadenas, bytes, long etc....
Public Function limiteB(matriz As Variant) As Integer
If Not IsArray(matriz) Then
matriz = Array()
End If
limiteB = UBound(matriz)
End Function
Private Sub Form_Load()
FraMetodo.Tag = 0
End Sub
Private Sub HScrRep_Change()
Me.LabRep.Caption = "Repeticiones: " & HScrRep.Value
End Sub
Private Sub OptMetodo_Click(Index As Integer)
FraMetodo.Tag = Index
End Sub