From: Arnaud Giersch Date: Wed, 22 Dec 2010 08:23:09 +0000 (+0100) Subject: Use numerical algorithms for stddev computation. X-Git-Tag: v0.1~204 X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/loba.git/commitdiff_plain/7725e3d7f34e70c0ba01ddc4e1055e096e72c2c3?ds=sidebyside;hp=-c Use numerical algorithms for stddev computation. --- 7725e3d7f34e70c0ba01ddc4e1055e096e72c2c3 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); }