Finding of time for function

I need to know time of function foo()
I cant find an avarage time of working for function (statistics.cpp)

experiment.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
#include <ratio>
#include <chrono>

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 me " << time_span.count() << " seconds";
    std::cout << std::endl;
    return 0;
}


statistics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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;
}


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
#include <iostream>
#include "experiment.h"
#include "statistics.h"
#include <chrono>
#include <ratio>

int main(){
    experiment ex;
    using namespace std::chrono;
    std::cout << "wait...." << std::endl;
    const size_t n = 10; //000;
    double arr[n];
    for (size_t i = 0; i < n; ++i)    {
        const auto dt = ex.measureTime(10);

        arr[i] = duration_cast<duration<double>>(dt).count(); //there is a mistake
        std::cout << i << ": " << arr[i] << " sec" << std::endl;
    }
    statistics stat;
    std::cout << "Average time is " << stat.meanTime(arr, n) << " sec" << std::endl;
    //std::cout << "Std time is " << stdTime(arr, n); << " sec" << std::endl;

}
Last edited on
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
what I see in your code:

1
2
3
4
5
6
7
8
9
10
    double experiment::measureTime(int n) 
{
    return 0;
}


 for (size_t i = 0; i < n; ++i)    
{
        const auto 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.
Last edited on
what I see in your code
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?

experiment.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 "experiment.h"
#include <chrono>
#include <iostream>
#include <thread>

using namespace std;
using namespace 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;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "experiment.h"
#include "statistics.h"

int main(){

    int i{ 0 };
    int z{ 10 };
    size_t n{ 10 };
    
    experiment ex;   
    statistics stat;

    double* t = new double[i];
    for (size_t i = 0; i < n; ++i)  ex.measureTime(i);   
    
    stat.meanTime(t, n);

    delete[] t;
}
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.
Last edited on
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;
}


I dont know how to do it in main
Last edited on
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);
Last edited on
maybe what you want is line 15
for (size_t i = 0; i < n; ++i) t[i] = ex.measureTime(i);


Yes. I tried it but I has an exeption (if Im not wrong now). I dont understend why It is
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.


http://cpp.sh/3r24c

When debugging code, spend some time looking at each line and think, what are my assumptions here, and then figure out a way to test them.
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.


http://cpp.sh/3r24c

When debugging code, spend some time looking at each line and think, what are my assumptions here, and then figure out a way to test them.


Thank you very much!
Topic archived. No new replies allowed.