for versus foreach
Walter Bright
newshound at digitalmars.com
Sat Apr 8 21:58:17 PDT 2006
Ameer Armaly wrote:
> What are the reasons/differences in using foreach over a for loop? Is
> it just performance or readability?
By using foreach, you are letting the compiler decide on the
optimization rather than worrying about it yourself. For example - are
pointers or indices better? Should I cache the termination condition or
not? Should I rotate the loop or not? The answers to these questions are
not easy, and can vary from machine to machine. Like register
assignment, let the compiler do the optimization.
for (int i = 0; i < foo.length; i++)
or:
for (int i = 0; i < foo.length; ++i)
or:
for (T *p = &foo[0]; p < &foo[length]; p++)
or:
T *end = &foo[length];
for (T *p = &foo[0]; p < pend; ++p)
or:
T *end = &foo[length];
T *p = &foo[0];
if (p < pend)
{
do
{
...
} while (++p < end);
}
and, of course, should I use size_t or int?
for (size_t i = 0; i < foo.length; i++)
Let the compiler pick!
foreach (v; foo)
...
Note that we don't even need to know what the type T needs to be, thus
avoiding bugs when T changes. I don't even have to know if foo is an
array, or an associative array, or a struct, or a collection class. This
will also avoid the common fencepost bug:
for (int i = 0; i <= foo.length; i++)
And it also avoids the need to manually create a temporary if foo is a
function call.
The only reason to use a for loop is if your loop does not fit in the
conventional form, like if you want to go through the array backwards,
or change the termination condition on the fly.
More information about the Digitalmars-d-learn
mailing list