Skip to content

Commit

Permalink
Merge pull request #2 from mhouppin/add_stddev_display
Browse files Browse the repository at this point in the history
Add standard deviation to the displayed stats
  • Loading branch information
SimonCROS authored Jan 6, 2023
2 parents a0146c1 + c5ac558 commit 2a45756
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion includes/complexity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ string exec(char **argv, optional<string> input = nullopt);

// Print
void printStart(const program_opts& opts, const program_params& params);
void print(const program_params& params, int done, int total, int best, int worst, int successful, int ok);
void print(const program_params& params, int done, int mean, double stddev, int best, int worst, int successful, int ok);
void printEnd(const program_opts& opts, const program_params& params);

#endif
6 changes: 4 additions & 2 deletions src/lang/en_GB.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "complexity.hpp"

#include <iomanip>
#include <iostream>

using namespace std;
Expand Down Expand Up @@ -42,10 +43,11 @@ void printStart(const program_opts& opts, const program_params& params) {
cout << endl;
}

void print(const program_params& params, int done, int total, int best, int worst, int successful, int ok) {
void print(const program_params& params, int done, int mean, double stddev, int best, int worst, int successful, int ok) {
cout << "Worst = \033[31m" << (worst) << "\033[0m instructions" << endl;
cout << "Median = \033[33m" << (total / done) << "\033[0m instructions" << endl;
cout << "Median = \033[33m" << (mean) << "\033[0m instructions" << endl;
cout << "Best = \033[36m" << (best) << "\033[0m instructions" << endl;
cout << "Std. deviation = \033[93m" << setiosflags(ios_base::fixed) << setprecision(1) << (stddev) << "\033[0m instructions" << endl;
if (params.objective.has_value())
cout << "Objective = \033[94m" << (successful * 100 / done) << "\033[0m % under \033[94m" << (params.objective.value()) << "\033[0m (\033[91m" << (done - successful) << "\033[0m above) " << endl;
else
Expand Down
6 changes: 4 additions & 2 deletions src/lang/fr_FR.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "complexity.hpp"

#include <iomanip>
#include <iostream>

using namespace std;
Expand Down Expand Up @@ -42,10 +43,11 @@ void printStart(const program_opts& opts, const program_params& params) {
cout << endl;
}

void print(const program_params& params, int done, int total, int best, int worst, int successful, int ok) {
void print(const program_params& params, int done, int mean, double stddev, int best, int worst, int successful, int ok) {
cout << "Pire = \033[31m" << (worst) << "\033[0m instructions" << endl;
cout << "Moyenne = \033[33m" << (total / done) << "\033[0m instructions" << endl;
cout << "Moyenne = \033[33m" << (mean) << "\033[0m instructions" << endl;
cout << "Meilleur = \033[36m" << (best) << "\033[0m instructions" << endl;
cout << "Écart-type = \033[93m" << setiosflags(ios_base::fixed) << setprecision(1) << (stddev) << "\033[0m instructions" << endl;
if (params.objective.has_value())
cout << "Objectif = \033[94m" << (successful * 100 / done) << "\033[0m % sous \033[94m" << (params.objective.value()) << "\033[0m (\033[91m" << (done - successful) << "\033[0m au dessus) " << endl;
else
Expand Down
18 changes: 15 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "complexity.hpp"

#include <cmath>
#include <iostream>
#include <vector>
#include <numeric>
Expand Down Expand Up @@ -39,11 +40,14 @@ void launchTest(program_opts& opts, program_params& params) {

vector<string> args;
vector<char*> cargs;
vector<int> results_list;

args.reserve(params.numbers);
cargs.reserve(params.numbers + 2);
results_list.reserve(params.iterations);

int done = 0, worst = 0, best = -1, total = 0, successful = 0, ok = 0;
double mean, stddev;

hideCursor();
while (done < params.iterations) {
Expand All @@ -53,6 +57,7 @@ void launchTest(program_opts& opts, program_params& params) {
int lines = std::count(result.begin(), result.end(), '\n');

done++;
results_list.push_back(lines);
total += lines;

if (params.checker.has_value()) {
Expand All @@ -70,8 +75,15 @@ void launchTest(program_opts& opts, program_params& params) {
if (lines > worst)
worst = lines;

print(params, done, total, best, worst, successful, ok);
cout << "\033[6A";
mean = double(total) / done;
stddev = 0.0;
for (int result : results_list)
stddev += pow(mean - result, 2.0);

stddev = sqrt(stddev / done);

print(params, done, round(mean), stddev, best, worst, successful, ok);
cout << "\033[7A";
}

if (params.checker.has_value() && (done - ok > 0))
Expand Down Expand Up @@ -123,7 +135,7 @@ int main(int argc, char **argv) {
params.iterations = 1;

std::atexit([]() {
cout << "\033[6B\033[0m";
cout << "\033[7B\033[0m";
showCursor();
});
signal(SIGINT, [](int) {
Expand Down

0 comments on commit 2a45756

Please sign in to comment.