|
-
Dec 8th, 2004, 02:46 AM
#1
Thread Starter
Banned
some stuff i want to know about collections
is a collection faster than an array?
and wasnt there a find member, or was that in a difrent type of collection...?
Last edited by nareth; Dec 8th, 2004 at 03:47 PM.
-
Dec 8th, 2004, 02:50 AM
#2
Re: some stuff i want to know about collections
just a guess, but I'd imagine that an array would be faster, as it is in the same memory location.
-
Dec 8th, 2004, 03:00 AM
#3
Thread Starter
Banned
Re: some stuff i want to know about collections
-
Dec 8th, 2004, 03:12 AM
#4
Re: some stuff i want to know about collections
contiguous memory location? anyway, once they are initialized, arrays just have a pointer that jumps the same number of bytes to the next value.
-
Dec 8th, 2004, 04:21 AM
#5
Thread Starter
Banned
Re: some stuff i want to know about collections
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?
-
Dec 8th, 2004, 04:25 AM
#6
Re: some stuff i want to know about collections
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.
-
Dec 8th, 2004, 04:34 AM
#7
Thread Starter
Banned
Re: some stuff i want to know about collections
i dont see a find member how do i search for key...?
-
Dec 8th, 2004, 07:02 AM
#8
Re: some stuff i want to know about collections
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")
-
Dec 8th, 2004, 08:42 AM
#9
Thread Starter
Banned
Re: some stuff i want to know about collections
oooh ok. thanks this helps ALOOOT
so is it very slower than array or not?
-
Dec 8th, 2004, 08:55 AM
#10
Thread Starter
Banned
Re: some stuff i want to know about collections
omg arrays are SLOWER!!!!!!
Code:
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
A.A;953
B.A;1110
-
Dec 8th, 2004, 12:36 PM
#11
Re: some stuff i want to know about collections
omg arrays are SLOWER!!!!!!
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.
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).
-
Dec 8th, 2004, 01:50 PM
#12
Re: some stuff i want to know about collections
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.
-
Dec 8th, 2004, 01:58 PM
#13
Thread Starter
Banned
Re: some stuff i want to know about collections
 Originally Posted by si_the_geek
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.
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).
a collection is dynamic so the array should be dynamic to or else to test is not fair
-
Dec 8th, 2004, 03:09 PM
#14
Frenzied Member
Re: some stuff i want to know about collections
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
Code:
Call A.Add(I, Str(I))
needed to be
Code:
Call A.Add(I, CStr(I))
Interesting though. Thanks for kicking off the thread
-
Dec 8th, 2004, 03:16 PM
#15
Thread Starter
Banned
Re: some stuff i want to know about collections
Wierd!.....
i modified the code again to be fair 100%
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
guess what i came up with???
A;62
B;1125
-
Dec 8th, 2004, 04:02 PM
#16
Frenzied Member
Re: some stuff i want to know about collections
lol - you couldn't have. Both Debug prints would have shown A;
-
Dec 8th, 2004, 04:10 PM
#17
Frenzied Member
Re: some stuff i want to know about collections
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 ? 
Code:
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
A;120
B;1933
C;30
-
Dec 8th, 2004, 04:17 PM
#18
Thread Starter
Banned
Re: some stuff i want to know about collections
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
-
Dec 8th, 2004, 04:18 PM
#19
Thread Starter
Banned
Re: some stuff i want to know about collections
and you can get the ubound by simply calling UBound(arr)
and collection has betetr functionality
-
Dec 8th, 2004, 04:26 PM
#20
Frenzied Member
Re: some stuff i want to know about collections
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|