Results 1 to 4 of 4

Thread: [RESOLVED] Removing multiple items of a collection, which way is faster?

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [RESOLVED] Removing multiple items of a collection, which way is faster?

    Hi,
    my Objects are maontained in a List (of T).
    In each TimeCycle I need to check each item and remove it if a criteria is met.
    I do hesitate do remove within the main For..Each loop, in order not to make the for..Each stumble.

    I came up with two solutions:
    Method A
    Code:
    'in main For Each
    For Each a As myObject in ObjectList
    ..
    If Criteria Then
       RemovList.Add(a)
    End If
    
    Next 
    For Each a As myObject in RemoveList
       ObjectList.Remove(a)
    Next
    Should be fast in the Main For..Next, however needs a loop to remove.
    Isn't it possible to do such a Remove without a For Next?

    Method B
    Code:
    'after Main For Next
    
    ObjectList.RemoveAll(AddressOf CriteriaMet)
    
    ...
    'Elsewhere
    Private Shared Function CriteriaMet(ByVal a as myObject) As Boolean
     Return a.someProperty=WhatEver
    End Function
    Nothing to do within Main For..Next, however the Removeall has to be called in each Timecycle. I do believe the removeAll iterates through all items like a For...Next loop.

    Any suggestions??
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

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

    Re: Removing multiple items of a collection, which way is faster?

    One way which should be slightly quicker is to switch from For-Each to a 'backwards' For, eg:
    Code:
    For index as Integer = ObjectList.Count -1 To 0 Step -1
      Dim a As myObject = ObjectList(Index)
      ..
      If Criteria Then
        ObjectList.RemoveAt(index)
      End If
    Next

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Removing multiple items of a collection, which way is faster?

    Good point,

    so I'll change the main For..Each into a backwards For..Next and Remove the Items rigth in this loop. This way there will only be a single loop (visible or hidden in RemoveAll etc.)
    I think that is the best solution.

    Thanks
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] Removing multiple items of a collection, which way is faster?

    Yep. RemoveAt should be faster because Remove still has to search the list for a match before removing it every time Remove is called.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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