|
-
Apr 19th, 2016, 09:07 AM
#1
Thread Starter
Lively Member
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
-
Apr 19th, 2016, 09:12 AM
#2
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.
-
Apr 19th, 2016, 09:16 AM
#3
Thread Starter
Lively Member
Re: How can I make this Loop quicker?
 Originally Posted by jmcilhinney
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.
-
Apr 19th, 2016, 09:22 AM
#4
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
-
Apr 19th, 2016, 09:39 AM
#5
Thread Starter
Lively Member
Re: How can I make this Loop quicker?
 Originally Posted by techgnome
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.
-
Apr 19th, 2016, 09:50 AM
#6
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
-
Apr 19th, 2016, 10:01 AM
#7
Thread Starter
Lively Member
Re: How can I make this Loop quicker?
 Originally Posted by FunkyDexter
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
-
Apr 19th, 2016, 10:22 AM
#8
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
-
Apr 19th, 2016, 10:24 AM
#9
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
-
Apr 19th, 2016, 10:58 AM
#10
Thread Starter
Lively Member
Re: How can I make this Loop quicker?
 Originally Posted by techgnome
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!
-
Apr 19th, 2016, 06:27 PM
#11
Re: How can I make this Loop quicker?
 Originally Posted by Paramalodux
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.
 Originally Posted by Paramalodux
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.
 Originally Posted by Paramalodux
FYI, I googled. A lot. I always google before I post.
And did you bother to use the VS Help menu?
 Originally Posted by Paramalodux
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.
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
|