From 7725e3d7f34e70c0ba01ddc4e1055e096e72c2c3 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Wed, 22 Dec 2010 09:23:09 +0100 Subject: [PATCH] Use numerical algorithms for stddev computation. --- main.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 8a23727..907869e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,7 @@ +#include #include #include +#include #include #include #include @@ -87,16 +89,19 @@ static void check_for_lost_load() static void compute_load_imbalance() { + using std::tr1::bind; + using std::tr1::placeholders::_1; + unsigned n = loads.size(); load_avg = std::accumulate(loads.begin(), loads.end(), 0.0) / n; - double variance = 0.0; - double epsilon = 0.0; - for (unsigned i = 0 ; i < n ; ++i) { - double diff = loads[i] - load_avg; - variance += diff * diff; - epsilon += diff; - } - variance = (variance - (epsilon * epsilon) / n) / n; + + std::vector diff(loads); + std::transform(diff.begin(), diff.end(), diff.begin(), + bind(std::minus(), _1, load_avg)); + double epsilon = std::accumulate(diff.begin(), diff.end(), 0.0); + double square_sum = std::inner_product(diff.begin(), diff.end(), + diff.begin(), 0.0); + double variance = (square_sum - (epsilon * epsilon) / n) / n; load_stddev = sqrt(variance); } -- 2.39.5