[fpc-pascal] for .. in loop implementation
Jürgen Hestermann
juergen.hestermann at gmx.de
Wed Jan 7 17:36:42 CET 2009
> I disagree with any statement saying that for .. in loop is
> only a type-saver. It's a good language extension and should be included
> (since Delphi already have this, it will also be a good idea). Consider the
> following example:
> for d in [Monday,Wednesday,Friday] do ;
> // versus
> for d:=Low(TDaySet) to High(TDaySet) do
> if d in [Monday,Wednesday,Friday] then ;
> The latter one has iteration overheads, while the former can be optimized to
> loop as many as needed. I'm not saying I'm the best Pascal programmer, but
> in case there's a (better) solution to this (rather than extending the
> language) please tell me.
I think there will be no performance difference because internaly the
compiler has to generate something like the first for-loop anyway. It
will only be hidden from the programmer and I am not sure whether this a
good thing.
Everything that obscures what's going on under the hood has the
potential to generate performance problems. In your example, you may not
be aware that at the end all possible members have to be checked for
beeing part of the set. If you only read
for d in [Monday,Wednesday,Friday] do ;
you may think that the loop is run through only 3 times, but internally
there are much more iterations (to check the "if d in
[Monday,Wednesday,Friday]").
Jürgen Hestermann.
More information about the fpc-pascal
mailing list