Hay algunas maneras, sin incluir una costumbre o COM objeto ActiveX
- ReDim preservar
- objeto Dictionary, que puede tener claves de las cadenas y la búsqueda de ellos
- ArrayList clases de .NET Framework, que tiene muchos métodos, incluyendo: especie (adelante, atrás, a medida), insertar, eliminar, binarySearch, es igual, toArray y toString
Con el código de abajo, me encontré con ReDim Preserve es más rápido por debajo de 54000, diccionario es Reducción del tiempo entre 54000 a 690000, y la lista de matriz se hace más rápido por encima de 690000. tiendo a utilizar ArrayList para empujar debido a la clasificación y la conversión de matriz.
user326639 proporcionan FastArray, que es bastante rápido.
Los diccionarios son útiles para buscar el valor y devolver el índice (es decir, nombres de campo) o para agrupar y agregar (histogramas, agrupar y agregar, agrupar y concatenar cadenas, agrupar y empujar sub-arreglos). Al agrupar en claves, establezca CompareMode para case in/sensitivity, y verifique la propiedad "exists" antes de "add" -ing.
Redim no ahorraría mucho tiempo para una matriz, pero es útil para un diccionario de matrices.
'pushtest.vbs
imax = 10000
value = "Testvalue"
s = imax & " of """ & value & """"
t0 = timer 'ArrayList Method
Set o = CreateObject("System.Collections.ArrayList")
For i = 0 To imax
o.Add value
Next
s = s & "[AList " & FormatNumber(timer - t0, 3, -1) & "]"
Set o = Nothing
t0 = timer 'ReDim Preserve Method
a = array()
For i = 0 To imax
ReDim Preserve a(UBound(a) + 1)
a(UBound(a)) = value
Next
s = s & "[ReDim " & FormatNumber(timer - t0, 3, -1) & "]"
Set a = Nothing
t0 = timer 'Dictionary Method
Set o = CreateObject("Scripting.Dictionary")
For i = 0 To imax
o.Add i, value
Next
s = s & "[Dictionary " & FormatNumber(timer - t0, 3, -1) & "]"
Set o = Nothing
t0 = timer 'Standard array
Redim a(imax)
For i = 0 To imax
a(i) = value
Next
s = s & "[Array " & FormatNumber(timer - t0, 3, -1) & "]" & vbCRLF
Set a = Nothing
t0 = timer 'Fast array
a = array()
For i = 0 To imax
ub = UBound(a)
If i>ub Then ReDim Preserve a(Int((ub+10)*1.1))
a(i) = value
Next
ReDim Preserve a(i-1)
s = s & "[FastArr " & FormatNumber(timer - t0, 3, -1) & "]"
Set a = Nothing
MsgBox s
' 10000 of "Testvalue" [ArrayList 0.156][Redim 0.016][Dictionary 0.031][Array 0.016][FastArr 0.016]
' 54000 of "Testvalue" [ArrayList 0.734][Redim 0.672][Dictionary 0.203][Array 0.063][FastArr 0.109]
' 240000 of "Testvalue" [ArrayList 3.172][Redim 5.891][Dictionary 1.453][Array 0.203][FastArr 0.484]
' 690000 of "Testvalue" [ArrayList 9.078][Redim 44.785][Dictionary 8.750][Array 0.609][FastArr 1.406]
'1000000 of "Testvalue" [ArrayList 13.191][Redim 92.863][Dictionary 18.047][Array 0.859][FastArr 2.031]
gracias por la ayuda! – Choy
Tenga en cuenta que 'Redim Preserve' copia toda la matriz cada vez que la usa. En otras palabras, tiene n ** 2 complejidad en la notación de O grande. – mgr326639
@user, copiar toda la matriz tiene una complejidad lineal ('O (n)'), no cuadrática ('O (n²)'). –