comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@cs.nyu.edu (Robert Dewar)
Subject: Re: Emulating Modula-3's FOR loop
Date: 1996/07/07
Date: 1996-07-07T00:00:00+00:00	[thread overview]
Message-ID: <dewar.836753316@schonberg> (raw)
In-Reply-To: 4rocup$rvo@ia.mks.com


David J. Fiander suggests for a loop jumping by N:

        begin
            while I <= Subscript'Last loop
                -- process
                I := I + N;
            end loop;
        exception
            when Constraint_Error => null;
        end;

This is definitely bad code. Using Constraint_Error to catch normal
flow of control situations as opposed to errors is a bad misuse of
the feature. Not only is it conceptually incorrect, but in practice
it can be disastrously inefficient (in many implementations, a
constraint error signal will involve a kernel trap with huge overhead).

A much better translation is

      loop
         -- process

         exit when I > Subscript'Last - N;
         I := I + N;
      end loop;

this should generate perfectly efficient code, since the expression
Subscript'Last - N is loop invariant. Note that this assumes you know
the loop will execute at least once. if not, the entire loop needs
to be protected by an appropriate test.





      reply	other threads:[~1996-07-07  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-07  0:00 Emulating Modula-3's FOR loop David J. Fiander
1996-07-07  0:00 ` Robert Dewar [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox