Anyway to make a FORLOOP PERFORM FASTER?
Hello everyone,
Is there anyway to make a for loop perform faster than it normally does? Cos,I have a project where I need to increase the speed of the application,precisely the For loop part.I haven't found any solutions yet.Will be glad if someone helps.
Thanks,
Godwin
Re: Anyway to make a FORLOOP PERFORM FASTER?
What is in the loop? A loop is only as fast as the things that are done inside.
Re: Anyway to make a FORLOOP PERFORM FASTER?
Quote:
Originally Posted by uniquegodwin
Hello everyone,
Is there anyway to make a for loop perform faster than it normally does? Cos,I have a project where I need to increase the speed of the application,precisely the For loop part.I haven't found any solutions yet.Will be glad if someone helps.
Thanks,
Godwin
There are many ways. But all depends on specific circumstances used in the programs.
e.g. if there are 10 objects indexed randomly say 1, 10, 12, 50, 100, 150, 175, 200, 250, 1000 etc, we don't need a for...next loop and loop 1000 times. We should instead use the for each...next loop so that we need to loop 10 times only.
Pradeep :)
Re: Anyway to make a FORLOOP PERFORM FASTER?
This one is basically to add '200000' words (sometimes numbers) to a listbox.Thats exactly what Im doing.So,how do I make this go faster?
Thanks :),
Godwin
Re: Anyway to make a FORLOOP PERFORM FASTER?
Add an entire row at one time. There were sveral fast techniques when we had the Prime number contest. Look in th econtest forums entries section as you will have several to choose from.
Re: Anyway to make a FORLOOP PERFORM FASTER?
Would you mind showing the loop? Its understandable if you dont, but it would help.
Re: Anyway to make a FORLOOP PERFORM FASTER?
There's always loop unrolling. However, what are the words in? It should be faster to put them in an array and assign the array to the listbox, but I think RD's suggestion is the best one.
ANother disconnected thought: You really don't want the listbox to try to update itself with every add. This is the idea behind adding an array at once, and probably the entire row idea from RD. Adding to an array is MUCH faster than adding to a listbox, and I suspect that has lots to do with events fired automatically every time you call Add. Even if the screen doesn't actually update with each one, I would be surprised if some preliminary events (like invalidating the listbox area) weren't fired with each Add.
Re: Anyway to make a FORLOOP PERFORM FASTER?
Correct Shaggy, those techniques are also shown in the contest entries. Using stuff like hiding the listbox during population, LockWindowUpdate, and adding from an array/range etc are all in there. :)
Re: Anyway to make a FORLOOP PERFORM FASTER?
I remember only that there WAS a contest, not what was done. :(
Re: Anyway to make a FORLOOP PERFORM FASTER?
Well I guess I remember because I entered and placed in the middle of the pack. I procrastinated and wrote it in a very short time. I could have done better if I spent more time on it. But we all knew Merri would win anyways :lol:
Re: Anyway to make a FORLOOP PERFORM FASTER?
Where are the econtest forums? econtest.com doesn't seem to be it. I don't think that hiding / preventing from repainting speeds up things because creating a new listbox (runtime) and showing it after it has been populated seems slower (30 sec instead of ~25 on a 1.5 GHz / 213,557 lines).
VB Code:
ListBox1.Items.Clear()
Dim Words() As String
Dim myReader1 As New StreamReader("c:\wordlist.txt")
Dim counter As Integer
Do While myReader1.Peek() <> -1
myReader1.ReadLine()
counter += 1
Loop
myReader1.Close()
ReDim Words(counter - 1)
'------------------------------------------
counter = 0
Dim myReader2 As New StreamReader("c:\wordlist.txt")
Do While myReader2.Peek() <> -1
Words(counter) = myReader2.ReadLine
counter += 1
Loop
myReader2.Close()
ListBox1.Items.AddRange(Words)
MessageBox.Show("Done!")
Re: Anyway to make a FORLOOP PERFORM FASTER?
Re: Anyway to make a FORLOOP PERFORM FASTER?
Re: Anyway to make a FORLOOP PERFORM FASTER?
Np, there is a sub forum in there where all the entries are kept. All members can view and download the entered source code.
Re: Anyway to make a FORLOOP PERFORM FASTER?
well,the other condition here is that the form shouldnt get stuck while performing the operation.If I use doevents or the background worker for the job,it reduces the speed than just normally doing it.If I simply use a forloop and update the listbox,the form totally freezes for a few seconds,but it goes fast.But now,what I need is that the same operation should go fast without the form freezing.
This is a process of adding email addresses/phone numbers from a text file onto the listbox.
Re: Anyway to make a FORLOOP PERFORM FASTER?
I checked out the contest stuff...some are in vb6 and some in vb.net...the projects there didnt have the problem of taking care of the freezing part as those were numbers within 1000.But,here,i need to do this without freezing the UI,at the same time,without comprimising on speed.
Re: Anyway to make a FORLOOP PERFORM FASTER?
Create a project and add a ListBox and three Buttons to the form. Now add the following code:
VB Code:
Public Class Form1
Private Const ITEM_COUNT As Integer = 5000
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.ListBox1.Items.Clear()
Dim startTime As Date = Date.Now
For i As Integer = 0 To ITEM_COUNT
Me.ListBox1.Items.Add(i)
Next
MessageBox.Show((Date.Now - startTime).TotalMilliseconds.ToString())
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.ListBox1.Items.Clear()
Dim startTime As Date = Date.Now
Dim items(ITEM_COUNT) As Object
For i As Integer = 0 To ITEM_COUNT
items(i) = i
Next
Me.ListBox1.Items.AddRange(items)
MessageBox.Show((Date.Now - startTime).TotalMilliseconds.ToString())
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.ListBox1.Items.Clear()
Dim startTime As Date = Date.Now
Me.ListBox1.BeginUpdate()
For i As Integer = 0 To ITEM_COUNT
Me.ListBox1.Items.Add(i)
Next
Me.ListBox1.EndUpdate()
MessageBox.Show((Date.Now - startTime).TotalMilliseconds.ToString())
End Sub
End Class
Press the three buttons and see the difference in the time taken to populate the ListBox. The first method is much slower because the ListBox gets redrawn for every item. The other methods redraw only once when all items have been added, so they are considerably faster.
If you have a long operation that you need to perform without freezing the UI then I'd suggest the second method with a BackgroundWorker. You populate an array using the BackgroundWorker and then you use AddRange to load the entire array into the ListBox in the RunWorkerCompleted event. That event is raised in the UI thread so there is no issue with cross-thread access to a control.
Edit: There will still be a certain amount of time associated with loading the data into the LisBox but the method I suggested will produce the shortest delay.
1 Attachment(s)
Re: Anyway to make a FORLOOP PERFORM FASTER?
Hi Jm,
That was cool...Thanks :)
But Jm,there is still a problem...it makes the user wait even if I put it using background worker and threading.
If I use DOevents(),It becomes very very slow although it works the right way.
I am trying to let the user see as the values get added to the listbox since im adding 200000 numbers.I've added threading and background worker to your code..attached it...Just take a look..
Thanks :)
Re: Anyway to make a FORLOOP PERFORM FASTER?
err..ive disabled the check for illegal cross thread operations...not sure if it has any side effects...and please press 1 button at a time or you'll notice number effects in the listbox ;)
Re: Anyway to make a FORLOOP PERFORM FASTER?