comp.lang.ada
 help / color / mirror / Atom feed
From: sdd.hp.com!cs.utexas.edu!ut-emx!hasan@hplabs.hpl.hp.com  (David A. Hasan)
Subject: Re: loop indices
Date: 20 Apr 92 21:11:35 GMT	[thread overview]
Message-ID: <70647@ut-emx.uucp> (raw)

In article <70628@ut-emx.uucp> hasan@ut-emx.uucp (David A. Hasan) writes:
>
>  DECLARE 
>    TYPE Index_1 IS (a,b,c,d,e,f,g);
>    TYPE Array_1 IS ARRAY(Index_Type1 RANGE <>) OF Integer;
>
>    TYPE Index_2 IS (aa,bb,cc,dd,ee,ff,gg);
>    TYPE Array_2 IS ARRAY(Index_Type2 RANGE <>) OF Integer;
>
>    v1 : Array_1( a..e );
>    v2 : Array_2( bb..ff );
>    i1 : Index_1;
>    i2 : Index_2;
>  BEGIN
>    i1 := v1'FIRST;
>    i2 := v2'FIRST;
>    FOR i IN 1..5 LOOP
>      v1(i1) := v2(i2);
>      IF i<5 THEN
>        i1 := Index_1'SUCC( i1 );
>        i2 := Index_2'SUCC( i2 );
>      END IF;
>    END LOOP;
>  END;
>
>My first question is this:  does this code carry a performance
>hit with it (relative to analogous code which works with
>'POS and 'VAL)?
>
At the expense of making a simple quesiont drag out
too long, I've got one data point here which might answer
my own question.  I see three general approaches to this
problem.  The are  

  i1 := first_element_in_v1;
  i2 := first_element_inv2;
  FOR i IN 1..number_of_elements LOOP
    v1(i1) := v2(i2);  
  EXIT WHEN i=number_of_elements;
    i1 := ...'SUCC( i1 );
    i2 := ...'SUCC( i2 );
  END LOOP;

or

  i1 := ...;
  i2 := ...;
  FOR ... LOOP 
    ...
    IF i<number_of_elements THEN
      --increment i1 & i2
    END IF;
  END LOOP;

or

  FOR i IN 1..number_of_elements LOOP
    v1( Index_1'VAL( Index_1'POS(v1'FIRST)+i-1 ) )
      :=
        v2( Index_2'VAL( Index_2'POS(v2'FIRST)+i-1 ) );
  END LOOP;

After looking at the VAX machine code (optimized for time),
I see that all three approaches involve four potential
branches inside the loop.  Based on this (admittedly crude)
analysis, I am tempted to conclude that I should not be
reluctant to use one of the first two (which I feel are 
easier to read), since the performance is roughly equivalent.

Any objections?

-- 
 |   David A. Hasan
 |   hasan@emx.utexas.edu 

             reply	other threads:[~1992-04-20 21:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-04-20 21:11 David A. Hasan [this message]
  -- strict thread matches above, loose matches on Subject: below --
1992-04-22 21:02 loop indices Robert I. Eachus
1992-04-20 16:39 David A. Hasan
1992-04-20 14:00 Robert Firth
1992-04-17 20:00 Mark A Biggar
replies disabled

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