Calling Dr Stack!

Calling Dr Stack!

let's say we want to calculate the of an array of #'s in a function. Traditionally one sets up loops to get the sum and the variance etc.
Consider the following alternative:

pure real(4) function getSdev(X,first,last)
integer,intent(in):: first,last
real(4),intent(in),volatile:: X(first:last)

1. Can the SUM function improve the speed over a looped sum?

2. Since we do not know the size of the array AND that X**2 is stored on the STACK, for a large array, can this cause problems in either in lack of stack size, or speed?


2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

As an aside, some authors recommend using two loops (or, I suppose, two SUM's). The first one calculates the mean, the second one sums (X-mean)**2. This way, you avoid roundoff errors (and the negative variances they produce sometimes).


Leave a Comment

Please sign in to add a comment. Not a member? Join today