>I would like to get your opinion about the difference in speed between the
>following two options and why do you prefer one over the other.
>
>Option 1
>--------
>Subroutine dosomething( a, b, c )
>IMPLICIT NONE
>REAL, INTENT(in) :: a(:,:)
>REAL, INTENT(in out) :: b(:,:), c(:)
>INTEGER :: i, j
> DO i = 1, UBOUND(a,1)
> DO j = 1,UBOUND(a,2)
> ....
> END DO
> END DO
>RETURN
>END SUBROUTINE
>
>Option 2
>--------
>Subroutine dosomething( a, b, c )
>IMPLICIT NONE
>REAL, INTENT(in) :: a(:,:)
>REAL, INTENT(in out) :: b(:,:), c(:)
>INTEGER :: i, j, length, width
> length = UBOUND(a,1)
> width = UBOUND(a,2)
> DO i = 1, length
> DO j = 1, width
> ....
> END DO
> END DO
>RETURN
>END SUBROUTINE
I would like to hope that any decent compiler would produce the same code. The
length variable, or its UBOUND as a DO parameter is executed once. The compiler
would know that. The width variable of the inner loop uses an INTENT(IN)
variable/array whose value should not change, and hence for the inner DO j =
1,UBOUND(a,2), the UBOUND value should be hoisted outside the outer loop. But
this might be a quality of implementation issue.
For readability, your assignments to length, width variables might have more
meaning to you a few years down the track or to a successor.
Regards, Paddy
Paddy O'Brien,
System Planning,
TransGrid,
PO Box A1000, Sydney South,
NSW 2000, Australia
Tel: +61 2 9284-3063
Fax: +61 2 9284-3050
Email: [log in to unmask]
Either "\'" or "\s" (to escape the apostrophe) seems to work for most people,
but that little whizz-bang apostrophe gives me little spam.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|