Results 1 to 20 of 20

Thread: some stuff i want to know about collections

  1. #1

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Resolved 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.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  3. #3

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: some stuff i want to know about collections

    same memory location...?

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  5. #5

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    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?

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    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.
    Frans

  7. #7

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: some stuff i want to know about collections

    i dont see a find member how do i search for key...?

  8. #8
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    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")
    Frans

  9. #9

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: some stuff i want to know about collections

    oooh ok. thanks this helps ALOOOT


    so is it very slower than array or not?

  10. #10

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    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

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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).

  12. #12

  13. #13

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    Re: some stuff i want to know about collections

    Quote 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

  14. #14
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    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
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  15. #15

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    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

  16. #16
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: some stuff i want to know about collections

    lol - you couldn't have. Both Debug prints would have shown A;
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  17. #17
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    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
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  18. #18

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    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

  19. #19

    Thread Starter
    Banned nareth's Avatar
    Join Date
    Jun 2004
    Posts
    1,206

    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

  20. #20
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    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.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width