Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Which loop take more time for of foreach?
Posted
Comments
virusstorm 14-May-15 13:09pm    
Why not write a simple program to test this?
Sergey Alexandrovich Kryukov 14-May-15 14:11pm    
C++? C? foreach? What are you asking about?
—SA
CPallini 14-May-15 15:45pm    
I can only guess the OP is asking of the C++ ranged for or (possibly the std::for_each)
Sergey Alexandrovich Kryukov 14-May-15 17:07pm    
Or C#, right? And not for C... I would avoid bravely answering the "question", as others did.
It's enough to say that the question is, strictly speaking, totally pointless.
"for" relies on indexed access, and "foreach" — on IEnumerable, both can be implemented in such a way to screw up all the performance.
Asking for preferences is just ridiculous, and the attempt to "optimize" something (even though it could be possible in each concrete case) just naive.
There is nothing to discuss seriously.
—SA
CPallini 14-May-15 15:47pm    
'for' is shorter than 'foreach' hence, it takes less time, at least in spelling it.

I assume you have a typo in your question and meant to ask "Which loop take more time for or foreach?"

Obviously, a for loop is a different thing than a foreach-loop and it's not always (easily) possible to exchange one for the other. In cases where it is easily possible, a for-loop is (usually) slightly faster than a foreach-loop because it (usually) just increments or decrements an integer variable but a foreach-loop requires an enumerator. Depending on what you do inside the loop, the for-loop can have additional performance-benefits.

I would suggest: If your code inside the loop isn't performance-critical use a foreach-loop if possible because it is (IMO) more intuitive to read. Otherwise use a for-loop if possible. And: Don't bother with optimization before you recognize that performance really is an issue.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-May-15 17:08pm    
You are right in some points (I voted 4), but I would not answer such question at all — please see my comment to the question, in reply to the comment by C Pallini.
—SA
Sascha Lefèvre 14-May-15 17:26pm    
Yes, I'm not 100% happy with it myself. I didn't want to go into too much detail but it seems the only other safe option then is to say "it depends" :) Anyway, thank you for your vote, Sergey.
Sergey Alexandrovich Kryukov 14-May-15 17:39pm    
Sure, but you may have a change to appreciate my advice for not answering some especially bad questions... :-)
—SA
Sascha Lefèvre 14-May-15 17:45pm    
Yes I must learn to resist ;-)
Sergey Alexandrovich Kryukov 14-May-15 17:46pm    
This is hard. :-)
—SA
" The decrementing for is faster than the incrementing for, which is faster than the foreach. This is often true regardless of what's being iterated.

...

Of primary importance is to always remember and never forget that the readability of foreach comes at the cost of being slower than for -- that really is a 20x difference between the two (yellow and green) techniques.
" -- On why to use DataViews[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-May-15 17:08pm    
You are right in some points, but I would not answer such question at all — please see my comment to the question, in reply to the comment by C Pallini.
—SA
You can see the benchmark results about this in the next article:
For Vs Foreach - Benchmark 2013[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-May-15 14:37pm    
Sorry, you referenced quite a poor article. Please see my comment to it.
The real answer would be: formulation of the question is not correct. Comparison with just two syntactic constructs is pointless. The loop code cannot be the same, and performance can greatly depend on the code. In case of foreach, it depends, for example, on the implementation of IDisposable.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900