foreach/iota countdown
Ivan Kazmenko
gassa at mail.ru
Tue Feb 18 02:30:17 PST 2014
>> /home/alaran/tmp/test.d(5:16)[warn]: 3 is larger than 2. This
>> slice is likely incorrect.
>> /home/alaran/tmp/test.d(6:22)[warn]: 20 is larger than 10. Did
>> you mean to use 'foreach_reverse( ... ; 10 .. 20)'?
>
> Isn't foreach_reverse being deprecated?
Oh. If so, what would be the right way to iterate backwards?
The usual "for (*;*;*)" is too repetitive and error-prone:
nothing is going to catch some "for (int i = 19; i >= 10; j--)"
errors or the like. Using ranges, such as iota and retro,
currently results in a *massive* slowdown for DMD.
As a crude proof for the last statement, below are my local
timings for DMD 2.064.2 on Win32 using "dmd -O -release -inline
-noboundscheck" to compile.
Example 1 (0.38 sec):
void main () {foreach (i; 0..1_000_000_000) {}}
Example 2 (0.39 sec):
void main () {for (int i = 0; i < 1_000_000_000; i++) {}}
Example 3 (2.03 sec):
import std.range;
void main () {foreach (i; iota (1_000_000_000)) {}}
Unless simple things like example 3 run on par with the first
two, foreach on ranges - or, in that regard, UFCS
functional-style range chains without even a foreach - are just
plain unacceptable in bottlenecks. LDC is able to handle simple
cases like this, but introducing a compiler dependency just
because of this? That would also be unfortunate.
Ivan Kazmenko.
More information about the Digitalmars-d-learn
mailing list