is a collection faster than an array?
and wasnt there a find member, or was that in a difrent type of collection...?
Printable View
is a collection faster than an array?
and wasnt there a find member, or was that in a difrent type of collection...?
just a guess, but I'd imagine that an array would be faster, as it is in the same memory location.
same memory location...?
contiguous memory location? anyway, once they are initialized, arrays just have a pointer that jumps the same number of bytes to the next value.
i like the remove member of a collection it helps me out alot. but is it alot slower than arrays or jsut a few ms?
The advantage of a collection is that you can also find an item by it's key. Arrays only have an index.
If you want to find an element in an array without knowing the index, you would have to loop through the array. This can take longer then accessing an item in a collection by it's key.
i dont see a find member how do i search for key...?
You can access an element of a collection by either it's index or it's key
eg
Code:Dim colCustomers As Collection
Set colCustomers = New Collection
colCustomers.Add "Frans Claessen", "Frans"
MsgBox colCustomers(1)
MsgBox colCustomers("Frans")
oooh ok. thanks this helps ALOOOT
so is it very slower than array or not?
omg arrays are SLOWER!!!!!!
A.A;953Code:Private Sub Form_Load()
Dim I As Long
Dim A As Collection
Set A = New Collection
Dim B()
ReDim B(0)
Dim C As Long
Let C = GetTickCount
For I = 0 To 100000
Call A.Add(I, Str(I))
Next
Debug.Print "A.A;" & GetTickCount - C
Let C = GetTickCount
For I = 0 To 100000
ReDim Preserve B(I)
Let B(I) = I
Next
Debug.Print "B.A;" & GetTickCount - C
End Sub
B.A;1110
only the way you have done it. ;) If you set the array size before the loop ("redim preserve" is very slow) you'll see that filling an array is much quicker.Quote:
omg arrays are SLOWER!!!!!!
Accessing items by position is also much faster with arrays than with collections.
Collections do have advantages though, as someone mentioned above you can find an item by it's key without having to loop. Also you can add/remove items in random places within the "list" whilst keeping it contiguous, which you cannot do with arrays (you need to move the rest of the items).
I love collections and I use them a lot, but keep in mind that a collection will take up more space than an array with same number of entries.
a collection is dynamic so the array should be dynamic to or else to test is not fairQuote:
Originally Posted by si_the_geek
I just ran your code nareth and got
A.A;2163
B.A;1933
Not much in it I have to admit BUT arrays showed up faster. BTW
needed to beCode:Call A.Add(I, Str(I))
Interesting though. Thanks for kicking off the threadCode:Call A.Add(I, CStr(I))
Wierd!.....
i modified the code again to be fair 100%
guess what i came up with???Code:Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Dim I As Long
Dim A As Collection
Set A = New Collection
Dim B()
Dim C As Long
Let C = GetTickCount
For I = 0 To 100000
Call A.Add(I)
Next
Debug.Print "A;" & GetTickCount - C
Let C = GetTickCount
For I = 0 To 100000
ReDim Preserve B(I)
Let B(I) = I
Next
Debug.Print "A;" & GetTickCount - C
End Sub
A;62
B;1125
lol - you couldn't have. Both Debug prints would have shown A;
Anyway. As I always define Ubound for my arrays, a fairer test (for fairer - read more applicable for everyday use) would have been C; Which as we would expect, comes out quicker. Interestingly enough bearing in mind this link, by a factor of 4. Now how jammy is that ? :bigyello:
A;120Code:Dim I As Long
Dim A As Collection
Set A = New Collection
Dim B()
Dim C As Long
Let C = GetTickCount
For I = 0 To 100000
Call A.Add(I)
Next
Debug.Print "A;" & GetTickCount - C
Let C = GetTickCount
For I = 0 To 100000
ReDim Preserve B(I)
B(I) = I
Next
Debug.Print "B;" & GetTickCount - C
Let C = GetTickCount
ReDim B(100000)
For I = 0 To 100000
B(I) = I
Next
Debug.Print "C;" & GetTickCount - C
B;1933
C;30
Let C = GetTickCount
ReDim B(100000)
For I = 0 To 100000
B(I) = I
Next
Debug.Print "C;" & GetTickCount - C
is NOT fair because you dont define a ubound to the collection
and you can get the ubound by simply calling UBound(arr)
and collection has betetr functionality
But A anc C compare everyday usage of the two functions.
You would be mad to code redim preserve on every entry, rather you would add X at a time and trim the array at the end. Where X is what you are comfortable with.
Anyway it is horses for courses.
An interesting thread, we should have a few more of these.