Can my standard deviation calculation be made more efficient?

Viewed 858

I'm curious if my standard deviation method can be made more efficient. By efficient I mean fast, and by fast I mean latency from method call to method return.

Here's the code:

public double stdDev(ArrayList<Double> input) {

    double Nrecip   = ( 1.0 / ( input.size()) );
    double sum      = 0.0;
    double average  = 0.0;

    for (Double input : inputs) {
        average += input;
    } average *= Nrecip;

    for (Double input : inputs) {
        sum += ( (input - average)*(input - average) );
    } sum *= Nrecip;

    return Math.sqrt(sum);

}

I would appreciate any advice.

2 Answers
Related