Loops versus ranges

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 19 03:25:22 PST 2014


aldanor:

> On Friday, 19 December 2014 at 10:57:47 UTC, aldanor wrote:
>> Something about the loop in the first case not depending on n 
>> and the compiler being able to figure it is out and only drop 
>> into recursion once?
> That's just a wild guess, but does it get transformed into 
> something like this?
>
> typeof(return) result;
> typeof(return) tmp = foo1(data, i + 1, max);
> foreach (immutable n; data)
>     result ~= tmp;

That's not the cause. You can see it if you add n to the foo 
functions, to remove that possible optimization:

foo1(data, n, i + 1, max);

(And the compiler is not allowed to perform that optimization 
because foos aren't strongly pure).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list