|
-
Dec 10th, 2006, 12:05 PM
#1
Thread Starter
Fanatic Member
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
Godwin
Help someone else with what someone helped you! 
-
Dec 10th, 2006, 12:11 PM
#2
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.
-
Dec 10th, 2006, 12:14 PM
#3
Re: Anyway to make a FORLOOP PERFORM FASTER?
 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
-
Dec 10th, 2006, 01:31 PM
#4
Thread Starter
Fanatic Member
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
Godwin
Help someone else with what someone helped you! 
-
Dec 10th, 2006, 02:42 PM
#5
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 10th, 2006, 02:57 PM
#6
Re: Anyway to make a FORLOOP PERFORM FASTER?
Would you mind showing the loop? Its understandable if you dont, but it would help.
-
Dec 10th, 2006, 03:05 PM
#7
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.
My usual boring signature: Nothing
 
-
Dec 10th, 2006, 03:10 PM
#8
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 10th, 2006, 03:33 PM
#9
Re: Anyway to make a FORLOOP PERFORM FASTER?
I remember only that there WAS a contest, not what was done.
My usual boring signature: Nothing
 
-
Dec 10th, 2006, 03:39 PM
#10
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 10th, 2006, 04:10 PM
#11
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!")
-
Dec 10th, 2006, 04:27 PM
#12
Re: Anyway to make a FORLOOP PERFORM FASTER?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 10th, 2006, 04:30 PM
#13
Re: Anyway to make a FORLOOP PERFORM FASTER?
-
Dec 10th, 2006, 04:31 PM
#14
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 10th, 2006, 09:12 PM
#15
Thread Starter
Fanatic Member
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.
Godwin
Help someone else with what someone helped you! 
-
Dec 10th, 2006, 09:17 PM
#16
Thread Starter
Fanatic Member
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.
Godwin
Help someone else with what someone helped you! 
-
Dec 10th, 2006, 09:41 PM
#17
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.
Last edited by jmcilhinney; Dec 10th, 2006 at 09:56 PM.
-
Dec 11th, 2006, 04:15 AM
#18
Thread Starter
Fanatic Member
-
Dec 11th, 2006, 04:17 AM
#19
Thread Starter
Fanatic Member
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
Godwin
Help someone else with what someone helped you! 
-
Dec 11th, 2006, 11:36 PM
#20
Thread Starter
Fanatic Member
Re: Anyway to make a FORLOOP PERFORM FASTER?
Godwin
Help someone else with what someone helped you! 
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
|