Results 1 to 10 of 10

Thread: Fastest method of looping?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2015
    Posts
    117

    Fastest method of looping?

    I am curious to know what the fastest way to loop is. Is it a for loop or a do While loop? Or maybe some other form of looping. I know that this largely depends on what is going on in the loop, but in general what is the fastest method?

  2. #2
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Fastest method of looping?

    Don't worry about it. It really depends on what you are doing - that would be the choice for type of loop.

    A for/next loop is possibly quickest, but if you have to do extra work to get the object you want then any advantage (if it exists) would be lost.

    Further, what loop type you use may be completely overridden by the compiler to optimize what is needed. This optimization may result in the same code regardless of the loop type your choose, or may actually end up making your code (marginally) slower.

    Bottom line, you would have to do millions/billions of iterations to really see any difference, and the difference is probably based on what you have in the loop and when it is run.

    You could even test it yourself!
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Fastest method of looping?

    If the slowest thing in your program is the actual looping mechanism, you've already finished optimization.

    In general, you can intuit that it goes in ascending order: Goto, For..Next, For Each. But compiler optimizations and JIT optimizations can render that ordering moot for thousands of reasons, and altering one line of code can completely reorder it.

    When this kind of optimization matters, you shouldn't be using a .NET language.

  4. #4
    Addicted Member
    Join Date
    Mar 2010
    Posts
    226

    Re: Fastest method of looping?

    All codes even virtual codes optimized for Assembly basics. As estimation for loops uses CX or ECX register for loops and it is fastest way for looping but in high level languages it is very hard to estimate which loop would be the fastest. If you use logical comparison in your code all magic is spoiled
    Regards
    algea

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Fastest method of looping?

    Back in the early days of VBA, MS said that there was an advantage of For...Next over For...Each. Subsequently, they said that they had improved the situations such that there is no longer a difference, and that's the current situation. I believe that Do will usually be slower than the others but only because the condition for jumping can be so much more complex.

    I do find it fun to check into things like this, and it's easy enough to do by using the Stopwatch object. You just create two methods that are identical except for the difference you are testing, then time each one of them a few times. You can't time them just once, though, because the JIT compiler will slow down the first method. I test each method twice in different order (which has never mattered any), so I do Test1, Test2, Test2, Test1. All that runs on a button press, and I hit the button a few times to see the results, which get reported in labels.

    You'll need tens or hundreds of millions of iterations to see any difference between the loops, though, which means it doesn't matter.
    My usual boring signature: Nothing

  6. #6
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Fastest method of looping?

    There are some optimization considerations for nested loops.
    e.g., if the function 'F' is expensive to call, then the first nested loop will perform much worse than the second since 'F' will be called a million times, while the second version will just call it once (but maybe it'll be optimized away by the compiler - no guarantee):

    Code:
    For i As Integer = 1 to 1000000
    	For j As Integer = 1 to F()
    	Next
    Next
    
    For j As Integer = 1 to F()
    	For i As Integer = 1 to 1000000
    	Next
    Next
    Edit: Actually, I just tried this where 'F' is a trivial function returning an integer literal and the first nested loop is considerably slower (due to the million function calls in version 1 vs just one function call in version 2) - so VB does not seem to optimize this.
    Last edited by David Anton; Jul 22nd, 2016 at 05:08 PM.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

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

    Re: Fastest method of looping?

    VB can't optimize that unless the situation is REALLY trivial. Partly for that reason, you generally won't see that kind of optimization EVER.

    The thing is that the compiler can't know whether F() returns a predictable value each time it is encountered. About the only way that it could would be if it was an inline method that returned a constant, and I'm not even sure that would be safe due to multithreading. So, the compiler pretty much HAS to evaluate F() for each iteration of the outer loop, and therefore you pay the whole price each time.
    My usual boring signature: Nothing

  8. #8
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Fastest method of looping?

    The urban legend around For loop optimizations I've seen is the compiler only performs optimizations if the target value is a literal/constant. If it's a non-constant variable of any form, even if it's a parameter, it assumes there could be an external change applied and checks the value every time. While it could go trekking around the code to figure this out, that's more a "static analysis" job than "an optimizing compiler". And if it gets it wrong, well, really interesting bugs happen.

    Every C++ developer I've met has a story about a time when their impossible-to-figure-out bug turned out to be an erroneous compiler optimization.

  9. #9
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Fastest method of looping?

    Quote Originally Posted by Sitten Spynne View Post
    ...

    Every C++ developer I've met has a story about a time when their impossible-to-figure-out bug turned out to be an erroneous compiler optimization.
    Which is why one should always use the tool that represents the functionality clearly and cleanly, rather than second-guessing optimizations.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Fastest method of looping?

    I have yet to see anything about what optimizations exist in .NET. I have yet to see optimizations impacting the super simplistic loops I use when testing trivial code construct speeds. I have also yet to hear of any optimizer bugs in .NET.

    For these reasons, I suspect that .NET does very little optimization and does so in an overly conservative fashion. For example, if you have this function:

    Code:
    Public Function Foo(incoming As Integer) As Integer
     Dim x = incoming
     Return 5
    End Function
    This could be optimized away to nothing without great difficulty, but I have yet to see that. The IL generated will, in fact, have the local variable x, even though it really isn't used, and the function will exist, even though it does nothing other than returns 5.
    My usual boring signature: Nothing

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