Is this code computationally economical?



Hello, All:

I am using Fortran 90 and have an array, a(1:N), whose some elements
are larger than 0, and others less than or equal to 0.

My task is to find the average of those elements that are larger than
0.

I am thinking to use the code

average=sum(a, mask=(a>0.0))/count(mask=(a>0.0))

But since I have to do it with enormous data and many similar
operations, I am wondering if it is as computationally economic as the
following do-loop with an imbedded if-loop:

s=0.0
n=0
do i=1, N
if(a(i)>0.0) then
s=s+a(i)
n=n+1
endif
enddo
average=s/n

I figure that the one-line code given above will have to go through all
the elements twice, and so it is not as computationally economical as
the loop. Is this correct?

Thank you for reading and replying!

--Roland

.



Relevant Pages

  • RE: complex number & sort
    ... i know how complex number must be written in fortran. ... with complex number in loop. ... i have an array with dimensioins A. ... numbers are calculated with 6 digits precesion ...
    (comp.lang.fortran)
  • complex number & sort
    ... i know how complex number must be written in fortran. ... with complex number in loop. ... i have an array with dimensioins A. ... numbers are calculated with 6 digits precesion ...
    (comp.lang.fortran)
  • Re: How to print out a Fortran array?
    ... Now I still have a question: I have an array holding real type of data, ... What version of Fortran are you using? ... loop can be replaced by a DO/ENDDO loop. ...
    (comp.lang.fortran)
  • Re: Is this code computationally economical?
    ... qquito wrote: ... I am using Fortran 90 and have an array, a, whose some elements ... n to 0 before the loop, ...
    (comp.lang.fortran)
  • Re: Is this code computationally economical?
    ... qquito wrote: ... I am using Fortran 90 and have an array, a, whose some elements ... the elements twice, and so it is not as computationally economical as ...
    (comp.lang.fortran)