From: Arnaud Giersch Date: Mon, 24 Jan 2011 17:04:05 +0000 (+0100) Subject: Define class statistics for computing sum, mean, and standard deviation. X-Git-Tag: v0.1~188^2~18 X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/loba.git/commitdiff_plain/825abbb3bde6c35ab1d1cdfba25fba4be5dcea2f?ds=sidebyside Define class statistics for computing sum, mean, and standard deviation. --- diff --git a/Makefile b/Makefile index 2760531..2b0df87 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,7 @@ SRC.loba := main.cpp \ neighbor.cpp \ options.cpp \ process.cpp \ + statistics.cpp \ version.cpp SRC.simple_async := simple_async.cpp diff --git a/main.cpp b/main.cpp index 5da797a..3a47b92 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,6 @@ -#include -#include #include -#include #include -#include #include -#include #include #include @@ -24,6 +19,7 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main); #include "misc.h" #include "options.h" #include "process.h" +#include "statistics.h" #include "timer.h" #include "tracing.h" #include "version.h" @@ -38,14 +34,9 @@ namespace { EXIT_FAILURE_CLEAN = 0x08, // error at cleanup }; - std::vector comps; - double comp_total; - double comp_avg; - double comp_stddev; + struct statistics comps; + struct statistics loads; - std::vector loads; - double load_avg; - double load_stddev; } static int simulation_main(int argc, char* argv[]) @@ -55,8 +46,8 @@ static int simulation_main(int argc, char* argv[]) try { proc = opt::loba_algorithms.new_instance(opt::loba_algo, argc, argv); result = proc->run(); - comps.push_back(proc->get_comp()); - loads.push_back(proc->get_load()); + comps.push(proc->get_comp()); + loads.push(proc->get_load()); delete proc; } catch (std::invalid_argument& e) { @@ -94,38 +85,10 @@ static void check_for_lost_load() total_running, running_ratio); } -static void statistics(const std::vector& vec, - double* sum, double* avg, double* stddev) -{ - using std::tr1::bind; - using std::tr1::placeholders::_1; - - unsigned n = vec.size(); - double vec_sum = std::accumulate(vec.begin(), vec.end(), 0.0); - double vec_avg = vec_sum / n; - - if (sum) - *sum = vec_sum; - if (avg) - *avg = vec_avg; - - if (stddev) { - std::vector diff(vec); - std::transform(diff.begin(), diff.end(), diff.begin(), - bind(std::minus(), _1, vec_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; - *stddev = sqrt(variance); - } -} - -static void compute_metrics() -{ - statistics(comps, &comp_total, &comp_avg, &comp_stddev); - statistics(loads, NULL, &load_avg, &load_stddev); -} +#define PR_STATS(descr, st) \ + INFO5("| %.*s: %g / %g / %g", 39, \ + descr " total/avg./stddev. at exit.........................", \ + st.get_sum(), st.get_avg(), st.get_stddev()) int main(int argc, char* argv[]) { @@ -235,12 +198,9 @@ int main(int argc, char* argv[]) if (simulated_time >= 0.0) { simulation_time.stop(); check_for_lost_load(); - compute_metrics(); INFO0(",----[ Results ]"); - INFO2("| Load avg./stddev. at exit..............: %g / %g", - load_avg, load_stddev); - INFO3("| Computation total/avg./stddev. at exit.: %g / %g / %g", - comp_total, comp_avg, comp_stddev); + PR_STATS("Load", loads); + PR_STATS("Computation", comps); INFO1("| Total simulated time...................: %g", simulated_time); INFO1("| Total simulation time..................: %g", simulation_time.duration()); diff --git a/statistics.cpp b/statistics.cpp new file mode 100644 index 0000000..77f415f --- /dev/null +++ b/statistics.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include + +#include "statistics.h" + +statistics::statistics() + : up2date(false) +{ +} + +void statistics::reset() +{ + values.clear(); + up2date = false; +} + +void statistics::push(double val) +{ + values.push_back(val); + up2date = false; +} + +size_t statistics::get_count() +{ + return values.size(); +} + +double statistics::get_sum() +{ + update(); + return sum; +} + +double statistics::get_avg() +{ + update(); + return avg; +} + +double statistics::get_stddev() +{ + update(); + return stddev; +} + +void statistics::update() +{ + if (up2date) + return; + + if (values.empty()) { + sum = 0.0; + avg = stddev = 0.0 / 0.0; + } + + using std::tr1::bind; + using std::tr1::placeholders::_1; + + unsigned n = values.size(); + sum = std::accumulate(values.begin(), values.end(), 0.0); + avg = sum / n; + + std::vector diff(values); + std::transform(diff.begin(), diff.end(), diff.begin(), + bind(std::minus(), _1, 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; + stddev = sqrt(variance); + + up2date = true; +} diff --git a/statistics.h b/statistics.h new file mode 100644 index 0000000..2978747 --- /dev/null +++ b/statistics.h @@ -0,0 +1,33 @@ +#ifndef STATISTICS_H +#define STATISTICS_H + +#include + +class statistics { +public: + statistics(); + + void reset(); + void push(double value); + + size_t get_count(); + double get_sum(); + double get_avg(); + double get_stddev(); + +private: + bool up2date; + + std::vector values; + double sum; + double avg; + double stddev; + + void update(); +}; + +#endif // !STATISTICS_H + +// Local variables: +// mode: c++ +// End: