Results 1 to 20 of 20

Thread: Anyway to make a FORLOOP PERFORM FASTER?

  1. #1

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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.
    VB 2005, Win Xp Pro sp2

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Anyway to make a FORLOOP PERFORM FASTER?

    Would you mind showing the loop? Its understandable if you dont, but it would help.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  11. #11
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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:
    1. ListBox1.Items.Clear()
    2.  
    3.         Dim Words() As String
    4.  
    5.         Dim myReader1 As New StreamReader("c:\wordlist.txt")
    6.         Dim counter As Integer
    7.  
    8.         Do While myReader1.Peek() <> -1
    9.             myReader1.ReadLine()
    10.             counter += 1
    11.         Loop
    12.  
    13.         myReader1.Close()
    14.         ReDim Words(counter - 1)
    15.         '------------------------------------------
    16.  
    17.         counter = 0
    18.         Dim myReader2 As New StreamReader("c:\wordlist.txt")
    19.  
    20.         Do While myReader2.Peek() <> -1
    21.             Words(counter) = myReader2.ReadLine
    22.             counter += 1
    23.         Loop
    24.         myReader2.Close()
    25.  
    26.         ListBox1.Items.AddRange(Words)
    27.         MessageBox.Show("Done!")
    VB 2005, Win Xp Pro sp2

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  13. #13
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Anyway to make a FORLOOP PERFORM FASTER?

    umpf

    Thanks, RobDog!
    VB 2005, Win Xp Pro sp2

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  15. #15

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

  16. #16

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

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

    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:
    1. Public Class Form1
    2.  
    3.     Private Const ITEM_COUNT As Integer = 5000
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Me.ListBox1.Items.Clear()
    7.  
    8.         Dim startTime As Date = Date.Now
    9.  
    10.         For i As Integer = 0 To ITEM_COUNT
    11.             Me.ListBox1.Items.Add(i)
    12.         Next
    13.  
    14.         MessageBox.Show((Date.Now - startTime).TotalMilliseconds.ToString())
    15.     End Sub
    16.  
    17.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    18.         Me.ListBox1.Items.Clear()
    19.  
    20.         Dim startTime As Date = Date.Now
    21.  
    22.         Dim items(ITEM_COUNT) As Object
    23.  
    24.         For i As Integer = 0 To ITEM_COUNT
    25.             items(i) = i
    26.         Next
    27.  
    28.         Me.ListBox1.Items.AddRange(items)
    29.  
    30.         MessageBox.Show((Date.Now - startTime).TotalMilliseconds.ToString())
    31.     End Sub
    32.  
    33.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    34.         Me.ListBox1.Items.Clear()
    35.  
    36.         Dim startTime As Date = Date.Now
    37.  
    38.         Me.ListBox1.BeginUpdate()
    39.  
    40.         For i As Integer = 0 To ITEM_COUNT
    41.             Me.ListBox1.Items.Add(i)
    42.         Next
    43.  
    44.         Me.ListBox1.EndUpdate()
    45.  
    46.         MessageBox.Show((Date.Now - startTime).TotalMilliseconds.ToString())
    47.     End Sub
    48.  
    49. 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.
    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

  18. #18

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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
    Attached Files Attached Files
    Last edited by uniquegodwin; Dec 11th, 2006 at 04:21 AM. Reason: To mention the 200000 value :)
    Godwin

    Help someone else with what someone helped you!

  19. #19

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

  20. #20

    Thread Starter
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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
  •  



Click Here to Expand Forum to Full Width