#include "statistics.h"
#include "cmath"
#include <iostream>
#include <chrono>
#include <ratio>
double statistics::meanTime(double* arr, size_t n){ //it should find an avarage time of working
double x = 0;
for (int i = 0; i < n; ++i)
x += arr[i];
return x / n;
}
double statistics::stdTime(double* t, size_t z){
for (int i = 1; i <= z; i += 1) {
*t += sqrt((1 / z) * pow((t[i] - *t), 2));
}
std::cout << "Standard deviation of time is " << t << std::endl;
return 0;
}
not going to bother to copy four files, one of which you haven't provided. upload to github or similar.
> double statistics::meanTime(double* arr, size_t n)
¿why limit it to time? that function can calculate the mean of the values of any array, they don't need to be time measures.
about your stdTime() function, don't do i/o on the function, it's not its responsibility, instead return a proper value
apart, ¿does your statistics class have any state? ¿why is it a class?
> there is a mistake
be more descriptive
¿wrong output? provide a testcase
double experiment::measureTime(int n)
{
return 0;
}
for (size_t i = 0; i < n; ++i)
{
constauto dt = ex.measureTime(10); //is zero!
arr[i] = duration_cast<duration<double>>(dt).count(); //still zero
FYI: cout is VERY SLOW in terms of clock cycles consumed. DO NOT USE COUT in your measuretime or other critical path parts of the study UNLESS you want to count that time spent. The cout time can be fixed by running it redirected to a file (program>filename) from the command line or setting up the same in your IDE if it supports setting up how to run it.
"cmath" looks like either a very badly named file you made or a mistake.
not going to bother to copy four files, one of which you haven't provided
Thanks!
I rewrote the code. Now It give me the time of function. Also I need to get an avarage time of working. It is meanTime function. But I have something like -7.84591e+297 in the result. How can I fix it?
#include "experiment.h"
#include <chrono>
#include <iostream>
#include <thread>
usingnamespace std;
usingnamespace std::chrono;
int experiment::foo(int n){
int s {0};
for (int i = 0; i <= n; i+=1) {
for (int j = 1; j <= n; j+=1) {
s += i * j;
}
}
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 me " << time_span.count() << " seconds\n";
return time_span.count();
}
statistics.cpp
1 2 3 4 5 6 7 8
double statistics::meanTime(double* t, size_t z){
double x = 0;
for (int i = 0; i < z; ++i) {
x += t[i];
}
std::cout << "Average time is " << x / z << " sec" << std::endl;
return x / z;
}
focus.
for (size_t i = 0; i < n; ++i) ex.measureTime(i);
where, exactly, does the RESULT of measureTime go?
did you mean to put it into a class variable? Did you mean to consume it (total += ex.measureTime()) with assignment?
You appear to now simply call the function in a loop (main line 15) and do ... what... with the time..?
also, you may need to work on your debugging skills. Try to find the problems yourself, using your tools.
also, be paranoid. make a sleep-for function and time it. You know about how long that would take, so you can verify your timing tools with it, then swap to to the real code once you are confident they work. Nothing is more annoying than having working code that you look at incorrectly then can't find the issue when the bug is in your debugging print statement or timer or whatever.
where, exactly, does the RESULT of measureTime go?
I need to take some results
It is
for (size_t i = 0; i < n; ++i) ex.measureTime(i);
Then I need to sum it and dividend on 10 (Average time)
double statistics::meanTime(double* t, size_t z){
double x = 0;
for (int i = 0; i < z; ++i) {
x += t[i];
}
std::cout << "Average time is " << x / z << " sec" << std::endl;
return x / z;
}
you need something like
for a bunch of times
totaltime = measureTime(n); //you lack the total variable!
...
average = totaltime /number of times
or, alternately, you need to populate the array that meantime uses. you never put values into it. maybe what you want is line 15
for (size_t i = 0; i < n; ++i) t[i] = ex.measureTime(i);
As some others have pointed out, you are not storing the results of the measure time function. As can be seen by adding some debug. http://cpp.sh/2uda3
The reason you were getting an exception with the line above is the arrany t was initialised with size 0.
As some others have pointed out, you are not storing the results of the measure time function. As can be seen by adding some debug. http://cpp.sh/2uda3
The reason you were getting an exception with the line above is the arrany t was initialised with size 0.