Re: foreach vs. for

From: Justin Rogers (Justin_at_games4dotnet.com)
Date: 03/28/04


Date: Sat, 27 Mar 2004 23:30:08 -0800

You can read my full examination of these methods on my blog. for and
foreach under most circumstances have the same performance results, even
though they have different code. The semantics of foreach (storing a local
version of the array and a local copy of the array element) are required,
while you are allowed to manage these options yourself when using a for
loop.

http://weblogs.asp.net/justin_rogers/archive/2004/03/26/97230.aspx

-- 
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"cody" <please_dont.spam.deutronium@gmx.de> wrote in message
news:edP$bXAFEHA.3344@tk2msftngp13.phx.gbl...
> Also I wonder what the conv.i4 instruction is needed for. the lenght
> property of array already returns an int so there is no need to convert
> someting. I also noted that the conv instruction was apparent in the for()
> version also.
>
> decompilation of the loop returned following code:
>
>  for (int k5 = 0; k5 < (int)nums2.Length; k5++)
>       {
>         int k1 = nums2[k5];
>         WriteLine("test");
>       }
>
> note the cast of the length property. also there is an unnessacary
> assignment of k1.
>
> -- 
> cody
>
> [Freeware, Games and Humor]
> www.deutronium.de.vu || www.deutronium.tk
>
>


Relevant Pages

  • Re: foreach vs. for
    ... You can read my full examination of these methods on my blog. ... The semantics of foreach (storing a local ... version of the array and a local copy of the array element) are required, ... I also noted that the conv instruction was apparent in the for> version also. ...
    (microsoft.public.dotnet.general)
  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)
  • Re: [PHP] Foreach
    ... The problem I am having is getting both to update the table in MySQL. ... echo "$t"; ... foreach { ... // foreach expects an array, ...
    (php.general)

Quantcast