for versus foreach
Dave
Dave_member at pathlink.com
Sat Apr 8 12:28:08 PDT 2006
Ameer Armaly wrote:
> What are the reasons/differences in using foreach over a for loop? Is
> it just performance or readability?
>
I've rarely seen it either increase or decrease performance by any
significant degree in my benchmark code, but I suppose(?) future
compilers could more easily optimize foreach loops to unroll and/or
vectorize with SIMD because of the semantics of foreach loops (local key
and value variables, array length is available, etc.).
I find them easier to read and now more concise to write too since
key/value inference was added.
foreach should make it harder to overrun buffers (either unintentionally
or as an attack on the service or binary) too because there is always a
length component to D arrays. It won't prevent things like DOS attacks
on buffers made of dynamic arrays, but foreach should generally make
looping over D arrays safer and more secure if you can use it.
- Dave
> --
>
>
> Ameer
> ---
> Visit my blog at
> http://ameerarmaly.blogspot.com
> ---
> Life is either tragedy or comedy.
> Usually it's your choice. You can whine or you can laugh.
> --Animorphs
More information about the Digitalmars-d-learn
mailing list