Re: Is this code computationally economical?



qquito wrote:
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

This really isn't the heart of the problem, but by setting
n to 0 before the loop, you've guaranteed that the loop
body will never be executed. Remember that Fortran is
not case sensitive.

Dick Hendrickson
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: 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)
  • 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)
  • Re: Coding in Fortran
    ... I would like to start some programming in fortran. ... a loop got performed at least once. ... That f77 feature was one of the last to be implemented uniformly. ...
    (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: About alternatives to Matlab
    ... But whether a single loop is faster than several BLAS calls does not ... Bad Fortran 95 can easily be 25% lower than well-tuned C99. ... NumPy can be slower than equivalent C by an order of magnitude. ... manipulated within Python, not the speed of python code using NumPy ...
    (comp.lang.python)