-
Jul 22nd, 2016, 09:17 AM
#1
Thread Starter
Lively Member
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?
-
Jul 22nd, 2016, 09:45 AM
#2
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."
-
Jul 22nd, 2016, 09:45 AM
#3
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.
-
Jul 22nd, 2016, 10:17 AM
#4
Addicted Member
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
-
Jul 22nd, 2016, 11:18 AM
#5
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
-
Jul 22nd, 2016, 04:58 PM
#6
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.
-
Jul 24th, 2016, 08:35 PM
#7
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
-
Jul 24th, 2016, 09:38 PM
#8
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.
-
Jul 25th, 2016, 05:57 AM
#9
Re: Fastest method of looping?
Originally Posted by Sitten Spynne
...
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."
-
Jul 25th, 2016, 10:15 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|