Thread overview
Stride in foreach ranges
Aug 30, 2013
Paul Jurczak
Aug 30, 2013
Namespace
Aug 30, 2013
Paul Jurczak
Aug 30, 2013
Paul Jurczak
Aug 30, 2013
Dicebot
Aug 30, 2013
Paul Jurczak
August 30, 2013
Is there a better way to express a range with stride than this:

foreach (i; iota(0, N, 2))

Maybe something similar to F# syntax:

foreach (i; 0..2..N)

I found this thread suggesting syntax improvement http://forum.dlang.org/thread/bug-4112-3@http.d.puremagic.com/issues/
but I don't think it produced any results.

BTW, DMD produces faster code when for loop is used instead, with GDC it makes no difference.
August 30, 2013
On Friday, 30 August 2013 at 06:31:39 UTC, Paul Jurczak wrote:
> Is there a better way to express a range with stride than this:
>
> foreach (i; iota(0, N, 2))
>
> Maybe something similar to F# syntax:
>
> foreach (i; 0..2..N)
>
> I found this thread suggesting syntax improvement http://forum.dlang.org/thread/bug-4112-3@http.d.puremagic.com/issues/
> but I don't think it produced any results.
>
> BTW, DMD produces faster code when for loop is used instead, with GDC it makes no difference.

That is strange because the FAQ says here: http://dlang.org/faq.html#foreach

"[...]let the compiler do the optimization."

and

"Let the compiler pick!"

Can you prove this on DPaste?
August 30, 2013
On Friday, 30 August 2013 at 06:31:39 UTC, Paul Jurczak wrote:
> BTW, DMD produces faster code when for loop is used instead, with GDC it makes no difference.

This pretty much shows that there is nothing wrong in language with current iota approach and it is simply problem of bad dmd code generation.
August 30, 2013
On Friday, 30 August 2013 at 14:11:06 UTC, Namespace wrote:
> On Friday, 30 August 2013 at 06:31:39 UTC, Paul Jurczak wrote:
>> Is there a better way to express a range with stride than this:
>>
>> foreach (i; iota(0, N, 2))
>>
>> Maybe something similar to F# syntax:
>>
>> foreach (i; 0..2..N)
>>
>> I found this thread suggesting syntax improvement http://forum.dlang.org/thread/bug-4112-3@http.d.puremagic.com/issues/
>> but I don't think it produced any results.
>>
>> BTW, DMD produces faster code when for loop is used instead, with GDC it makes no difference.
>
> That is strange because the FAQ says here: http://dlang.org/faq.html#foreach
>
> "[...]let the compiler do the optimization."
>
> and
>
> "Let the compiler pick!"
>
> Can you prove this on DPaste?

It's there at: http://dpaste.dzfl.pl/2cf504db
August 30, 2013
On Friday, 30 August 2013 at 17:19:11 UTC, Paul Jurczak wrote:
> On Friday, 30 August 2013 at 14:11:06 UTC, Namespace wrote:
[..]
>> Can you prove this on DPaste?
>
> It's there at: http://dpaste.dzfl.pl/2cf504db

I'm not sure what are the compiler switches on DPaste and I don't see an option to change them, so here are my measurements:

Windows 7 64-bit Core i5 2500 3.4GHz turbo:
DMD 2.063.2 (32-bit): dmd -O -noboundscheck -inline -release
669171001   622ns  e28_0
669171001  1244ns  e28_1

Xubuntu 13.04 64-bit Core i5 3450S 2.8GHz (3.5GHz turbo):
GDC 4.8.1:     gdc -m64 -march=native -fno-bounds-check -frename-registers -frelease -O3
669171001  1455ns  e28_0
669171001  1458ns  e28_1

DMD64 2.063.2: dmd -O -noboundscheck -inline -release
669171001  891ns  e28_0
669171001  1572ns  e28_1


August 30, 2013
On Friday, 30 August 2013 at 14:18:22 UTC, Dicebot wrote:
> On Friday, 30 August 2013 at 06:31:39 UTC, Paul Jurczak wrote:
>> BTW, DMD produces faster code when for loop is used instead, with GDC it makes no difference.
>
> This pretty much shows that there is nothing wrong in language with current iota approach and it is simply problem of bad dmd code generation.

DMD code generation is very good with for loops, not so good with foreach (see my timings in previous post).