For loop
-
Which for loop is more faster And Why? Can anyone suggest.. (i) Foreach I in Items //Your code Next I (ii) For int Counter = 0 ; Counter <ItemArray.Len() ; Counter++ //Your code
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman
-
Which for loop is more faster And Why? Can anyone suggest.. (i) Foreach I in Items //Your code Next I (ii) For int Counter = 0 ; Counter <ItemArray.Len() ; Counter++ //Your code
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman
Hi, it depends on the kind of collection. Could be faster, equal or slower. But never by much IMO. My advice is to write it in the way you feel most comfortable with. e.g. a
foreach(int x in intArray)
gets actually compiled exactly the same as afor(int x=0; x<intArray.Length; x++)
. others really call GetEnumerator, which (should) create an object that holds the current state of the enumeration and protect you against the collection being changed while enumerating. So they are not semantically identical. BTW: having ItemArray.Len() in the exit condition can be wasteful as it will be recalculated everytime, it might have been changed by some code somewhere. Better use a local variable (or a foreach!). If you have a specific case in mind, what is keeping you from: 1. performing a test run? 2. looking at the code (e.g. with Reflector)? :)Luc Pattyn
:badger: :jig: :badger:
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
:jig: :badger: :jig:
-
Which for loop is more faster And Why? Can anyone suggest.. (i) Foreach I in Items //Your code Next I (ii) For int Counter = 0 ; Counter <ItemArray.Len() ; Counter++ //Your code
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman
For an array it may not matter. For other collections it could go either way. Try both with the particular collection and see. Bear in mind that when using
foreach
, you're not allowed to alter the collection. If the collection might change as you're enumerating it,for
(orwhile
) may be your only choice. And as Luc mentioned, you may want to store the length in a local variable if retrieving it is costly and it doesn't change. Another option, if the order isn't important, is to run thefor
loop "backward":for ( int i = a.Len() - 1 ; i >= 0 ; i-- ) ...
-
For an array it may not matter. For other collections it could go either way. Try both with the particular collection and see. Bear in mind that when using
foreach
, you're not allowed to alter the collection. If the collection might change as you're enumerating it,for
(orwhile
) may be your only choice. And as Luc mentioned, you may want to store the length in a local variable if retrieving it is costly and it doesn't change. Another option, if the order isn't important, is to run thefor
loop "backward":for ( int i = a.Len() - 1 ; i >= 0 ; i-- ) ...
Thanks for your clarification…:)
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman
-
Thanks for your clarification…:)
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman
According to me the second (ii) forloop is faster than foreach.. because each time for loop checks the count and dosn't get the whole object, it just fix a index and navigate then takes a appropriate values from the bject collection. So as per the performance wise For loop is far better than foreach. :)
ZAK
-
According to me the second (ii) forloop is faster than foreach.. because each time for loop checks the count and dosn't get the whole object, it just fix a index and navigate then takes a appropriate values from the bject collection. So as per the performance wise For loop is far better than foreach. :)
ZAK
Your explanation is logical...Actually Im also agree with that point. :thumbsup: :)
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you. Thanks Md. Marufuzzaman