Results 1 to 11 of 11

Thread: How can I make this Loop quicker?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2015
    Posts
    73

    How can I make this Loop quicker?

    Hello, I got a loop which reads data from mysql server. My server is fine the speed and all but when it comes to looping around 5000 strings it's too slow.

    How can I make this loop quicker :



    Please help me out

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I make this Loop quicker?

    You didn't need to come here to ask that question. You'd already have your answer if you had read the documentation for the Add method you're calling. ALWAYS read the relevant documentation first.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2015
    Posts
    73

    Re: How can I make this Loop quicker?

    Quote Originally Posted by jmcilhinney View Post
    You didn't need to come here to ask that question. You'd already have your answer if you had read the documentation for the Add method you're calling. ALWAYS read the relevant documentation first.
    Not sure what you mean, why hold a grudge. Thanks for linking me to a source. FYI, I googled. A lot. I always google before I post. I didn't understand it so I decided to ask here. Now. bye.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How can I make this Loop quicker?

    don't add to the listview in the loop... add to a list(Of t) instead... then after the loop suspendlayout on the lsitview, addrange the list, then resumelayout.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2015
    Posts
    73

    Re: How can I make this Loop quicker?

    Quote Originally Posted by techgnome View Post
    don't add to the listview in the loop... add to a list(Of t) instead... then after the loop suspendlayout on the lsitview, addrange the list, then resumelayout.

    -tg
    Hello,

    First of all thankyou very much for helping. But I'm really not sure... Believe it or not this is the first time I'm using "For Each", Loops. I just recently discovered it. I'd appreciate it very much if you can provide me an example . I'll be googling what you meant, hopefully I can figure it out.

  6. #6
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How can I make this Loop quicker?

    You shouldn't really need an example because each step is pretty simple. A bit of an explanation might not go amiss though.

    Your original loop is slow because it updates the UI every time you add an item to the list view. Updating the UI is slow so your loop was slow. We're looking for a way of updating your list view... but only updating the UI once.

    So the first thing to understand is that a listview is displaying a List of ListItems. The fastest way to add a lot of elements to that List is to use the AddRange function. That will require you to pre-prepare a list of items to add as a range.

    Then you SuspendLayout on the ListView. This temporarily prevents the UI from being updated.

    Then you shove the items into your ListView using the AddRange function.

    And finally turn the UI back on so that your changes take effect in one hit.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2015
    Posts
    73

    Re: How can I make this Loop quicker?

    Quote Originally Posted by FunkyDexter View Post
    You shouldn't really need an example because each step is pretty simple. A bit of an explanation might not go amiss though.

    Your original loop is slow because it updates the UI every time you add an item to the list view. Updating the UI is slow so your loop was slow. We're looking for a way of updating your list view... but only updating the UI once.

    So the first thing to understand is that a listview is displaying a List of ListItems. The fastest way to add a lot of elements to that List is to use the AddRange function. That will require you to pre-prepare a list of items to add as a range.

    Then you SuspendLayout on the ListView. This temporarily prevents the UI from being updated.

    Then you shove the items into your ListView using the AddRange function.

    And finally turn the UI back on so that your changes take effect in one hit.
    Funky, how can I prepare listview items to be added as a range? The rest I understand. Thankyou very much

  8. #8
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: How can I make this Loop quicker?

    First create a List of type ListViewItem. Create individual ListViewItems (exactly as you do when using Add) and add them to the aforementioned List instead of to the ListView.Items collection. When you've added all the items you want you call the AddRange of the ListView as shown here:-
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    and pass your List into it as a parameter.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How can I make this Loop quicker?

    the same way you're doing it now, only instead of adding it to the LV directly, you add it to a list...

    Code:
    Dim lvItems as new List(Of ListViewItems) '<-- this is the list we'll add to
    'dang, you posted a picture of the code so I can't copy it...
    'start your for each -- you have that
    'create a new listviewtiem -- you have that
      lvItems.Add(lsv) ' see, here we add to the list, not the LV directl
    ' next
    lsitview1.suspendlayout ' -- this causes the control to not render everytime we add to it... it suspends the layout of hte control
    listivew1.items.addrange(lvItems) ' -- I think that works as-is ... at worst, you'll need lvItems.ToArray
    listveiw1.resumelayout ' we're done adding so now we release the control to update itself and it only needs to do it the once.
    I suppose you could keep the original code and simply suspendlayout before the loop and then resumelayout after the loop is done... but I typically will use the code with the addrange ...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2015
    Posts
    73

    Re: How can I make this Loop quicker?

    Quote Originally Posted by techgnome View Post
    the same way you're doing it now, only instead of adding it to the LV directly, you add it to a list...

    Code:
    Dim lvItems as new List(Of ListViewItems) '<-- this is the list we'll add to
    'dang, you posted a picture of the code so I can't copy it...
    'start your for each -- you have that
    'create a new listviewtiem -- you have that
      lvItems.Add(lsv) ' see, here we add to the list, not the LV directl
    ' next
    lsitview1.suspendlayout ' -- this causes the control to not render everytime we add to it... it suspends the layout of hte control
    listivew1.items.addrange(lvItems) ' -- I think that works as-is ... at worst, you'll need lvItems.ToArray
    listveiw1.resumelayout ' we're done adding so now we release the control to update itself and it only needs to do it the once.
    I suppose you could keep the original code and simply suspendlayout before the loop and then resumelayout after the loop is done... but I typically will use the code with the addrange ...

    -tg
    Oh my god, you're phenomenal thankyou! It worked just fine. Thankyou very much!

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I make this Loop quicker?

    Quote Originally Posted by Paramalodux View Post
    Not sure what you mean, why hold a grudge.
    I mean read the relevant MSDN documentation before doing ANYTHING else. I'm not sure what's difficult to understand about that. I'm not sure why my pointing out what you did wrong in failing to help yourself would be considered a grudge.
    Quote Originally Posted by Paramalodux View Post
    Thanks for linking me to a source.
    Why would I need to provide a link when you have the VS Help menu right in front of you? It's not like it's hidden.
    Quote Originally Posted by Paramalodux View Post
    FYI, I googled. A lot. I always google before I post.
    And did you bother to use the VS Help menu?
    Quote Originally Posted by Paramalodux View Post
    I didn't understand it so I decided to ask here. Now. bye.
    Had you done as you should then you'd have seen this:
    To add a set of items to the control in a single operation, use the AddRange method. If you want to use the Add method to add a large number of items to the control, use the BeginUpdate and EndUpdate methods to prevent the ListView from repainting until all items are added.
    Sounds fairly plain to me. If you'd then followed the links you'd have seen this:
    Code:
        ' Members are added one at a time, so call BeginUpdate to ensure 
        ' the list is painted only once, rather than as each list item is added.
        ListView1.BeginUpdate()
    
        For count = 0 To foodList.Length - 1
            Dim listItem As New ListViewItem(foodList(count))
            listItem.SubItems.Add(foodPrice(count))
            ListView1.Items.Add(listItem)
        Next
    
        'Call EndUpdate when you finish adding items to the ListView.
        ListView1.EndUpdate()
    Again, fairly plain. I'm not against people posting on forums or answering their questions but not if they haven't done the obvious things to help themselves first. It would have taken a few minutes at the most to find that information so there's really no excuse for not having done. I was a beginner too and that's exactly how I used to answer most of my own questions. When I first started posting here, I was also able to answer questions for others on unfamiliar topics by doing the same thing. If I can do it, everyone else can too. You can choose to heed this advice and help yourself to become better or you can feel hard done by and wonder why I'm being so mean to you. It depends what's more important to you I guess. Now. Bye.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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