Re: Is this code computationally economical?
- From: Dick Hendrickson <dick.hendrickson@xxxxxxx>
- Date: Sat, 27 Jan 2007 16:25:45 GMT
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
- References:
- Is this code computationally economical?
- From: qquito
- Is this code computationally economical?
- Prev by Date: Re: A file managment question
- Next by Date: Re: Pointer-based arrays output strange
- Previous by thread: Re: Is this code computationally economical?
- Next by thread: Re: Is this code computationally economical?
- Index(es):
Relevant Pages
|