#include <iostream>
#include <iomanip>
#include <chrono>
#include <cmath>

using namespace std;
using namespace chrono;

class Stat
{
public:
    Stat():x(0),x2(0),n(0){}
    void add(double y) { x += y; x2 += y*y; n++; }

    pair<double,double> get() const
    {
        pair<double,double> p;
        p.first = x/n;
        p.second = sqrt((n*x2-x*x)/n/(n-1));
        return p;
    }
private:
    double x, x2;
    int n;
};

class Experiment
{
public:
    Experiment(void (*f)(int), int cnt, int n)
    :f(f),cnt(cnt),n(n)
    {
    }
    pair<double,double> doit();
private:
    void (*f)(int);
    int cnt, n;
};


pair<double,double> Experiment::doit()
{
    using Clock = high_resolution_clock;
    Stat st;
    for(int i = 0; i < cnt; ++i)
    {
        Clock::time_point start_ = Clock::now();
        f(n);
        Clock::time_point stop_ = Clock::now();
        Clock::duration dt = stop_ - start_;
        st.add(static_cast<double>(duration_cast<microseconds>(dt).count()));
    }
    return st.get();
}

int sum = 0;  // Просто чтоб оптимизатор не выбросил...

void foo(int n)
{
    int s = 0;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n; ++j)
            s += i*j;
    sum += s;
}

int main()
{

    for(int n = 100; n < 1000; n+= 100)
    {
        Experiment ex(foo,
                      5, // Число повторов
                      n);
        auto p = ex.doit();

        cout << " N = " << fixed << setw(7) << n <<
            "   time =  " << fixed << setprecision(1) << setw(12) << p.first
            << " +- " << p.second << " mks\n";
    }
    cout << sum;
}

