BigInt foreach loop

Stefan Koch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 4 09:40:08 PDT 2017


On Friday, 4 August 2017 at 13:09:55 UTC, Steven Schveighoffer 
wrote:

> Any foreach range statement like this:
>
> foreach(var; A .. B)
>
> is treated as if you wrote:
>
> for(auto var = A; var < B; ++var)
>
> So it's pretty much exactly like what you wrote, just the 
> initializer is different but the result is the same.
>
> > as incrementing is a costly operation?
>
> Here is increment in bigint:
> https://github.com/dlang/phobos/blob/master/std/bigint.d#L563
>
> And addOrSubInt:
> https://github.com/dlang/phobos/blob/master/std/internal/math/biguintcore.d#L508
>
> I think there is room for improvement, as incrementing or 
> decrementing by 1 is probably something that can be optimized.
>
> -Steve

Not quite,
foreach(x;A .. B)
it's lowerd to
auto limit = B;
auto key = A;
for(auto x = key;key < limit;++key)
{
  // use x
}


More information about the Digitalmars-d-learn mailing list