There is a code:
statistics.h contains two function declarations:
meanTime - Returns the average time over an array (double).
stdTime - Returns the resulting standard deviation of time (double).
statistics.cpp provides the implementation of the above two functions.
experiment.h contains the following functions:
foo - function tested for speed
measureTime is a function that measures the running time of foo ().
experiment is the main work in our numerical experiment. It accepts an array of values of the function argument foo (int []), its size (size_t), the number of function starts with the same argument (size_t), an array into which the average running times will be written (double []) and an array for root-mean-square deviation of the operating time for a given number of starts for each value of the argument of the function under study (double []). Note that the size of these arrays must be the number of distinct argument values (arguments are the size of the n array). Each cell of type [i] should contain the average running time of the function foo (measured using measureTime) over z starts with the given argument arguments [i]. Likewise for stds [i], which should contain the standard deviation in time at the start of the foo function for a fixed argument of [i].
A function foo that takes an input argument n and calculates the sum ΣΣi*j
Its complexity is O (n ^ 2). The range of input values n must be from 1000 to 10000, the step is 1000, the number of retries z = 10.
Question: 1) I don't quite understand what needs to be written in the experiment function implementation
2) How to make n range from 1000 to 10000 (step equal to 1000)?
Now the code is working, when plotting the dependence of the average value on the standard deviations of the operating time, a parabola is obtained
experience.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include "experiment.h"
#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono;
int experiment::foo(int n) {
int s{ 0 };
for (int i = 1; i <= n; i += 1) {
for (int j = 1; j <= n; j += 1) {
s += i * j;
}
std::cout << "Sum is " << s << std::endl;
}
return 0;
}
double experiment::measureTime(int n) {
steady_clock::time_point t1 = steady_clock::now();
foo(n);
steady_clock::time_point t2 = steady_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
std::cout << "It took " << time_span.count() << " seconds\n";
return time_span.count();
}
void experiment::Experiment(int* arguments, size_t n, size_t z, double* means, double* stds){
}
|
statistics.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include "statistics.h"
#include "cmath"
#include <iostream>
double x = 0;
double statistics::meanTime(double* t, size_t z){
for (size_t i = 1; i <= z; ++i) x += t[i];
std::cout << "\t\tAverage time is " << x / z << " sec\n" << std::endl;
return x / z;
}
double statistics::stdTime(double* t, size_t z){
double STD{ 0 };
for (int i = 1; i <= z; i += 1) STD += (pow((t[i] - (x / z)), 2))/z;
std::cout << "\t\tStandard deviation of time is " << STD << std::endl;
return STD;
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <iostream>
#include "experiment.h"
#include "statistics.h"
int main() {
size_t n{ 10 };
size_t z{ 10 };
experiment ex;
statistics stat;
double* arguments = new double[11];
double* means = new double[11];
double* stds = new double[11];
for (size_t j = 1; j <= 10; ++j) {
std::cout << "[" << j << "]" << std::endl;
for (size_t i = 1; i <= 10; i += 1) {
arguments[i] = ex.measureTime(i);
}
means[j] = stat.meanTime(arguments, z);
stds[j] = stat.stdTime(arguments, z);
}
delete[] arguments;
delete[] means;
delete[] stds;
}
|