Storing elapsed time data in an arrat inside a for loop

#include <iostream>

#include <stack>

#include <ctime>

std::stack<clock_t> tictoc_stack;

void tic() {

tictoc_stack.push(clock());

}

void toc() {

std::cout << "Time elapsed: "

<< ((double((clock() - tictoc_stack.top())) / CLOCKS_PER_SEC

<< std::endl;

tictoc_stack.pop();




}

int main

{

tic();

for (k=0;k<10;k++)

{



doSomething();

toc();
dif=(clock() - tictoc_stack.top())
double t[10];


t[k]=dif;

std::cout<< "array is" << dif << std::endl;

}

}

return 0;

}

Now, when I call the toc() func it gives the elapsed time. but I want that time to go into

an array at each iteration. which means I would have 10 different elapsed time and I want

to get these values and calculate the mean, min, max and standard deviation.

So far I am having a hard time getting the elapsed to be stored in the array every time

the loop goes around.

Any Suggestions and help.
Sugestion, save the value into an array outside the for loop.
Something like this:

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
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <stack>
#include <ctime>

inline void tic( std::stack<std::clock_t>& tictoc_stack )
{ tictoc_stack.push( std::clock() ) ; }

std::vector<double> elapsed_intervals( std::stack<std::clock_t> tictoc_stack )
{
    std::vector<double> intervals ;
    const double TICKS_PER_SEC = CLOCKS_PER_SEC ;

    while( tictoc_stack.size() > 1 )
    {
        std::clock_t t = tictoc_stack.top() ;
        tictoc_stack.pop() ;
        intervals.push_back( ( t - tictoc_stack.top() ) * 1000 / TICKS_PER_SEC ) ;
    }

    return intervals ;
}

int main()
{
    std::stack<std::clock_t> tictoc_stack ;
    tic( tictoc_stack ) ;

    for( int k=0 ; k<10 ; ++k )
    {
         do_something() ;
         tic( tictoc_stack ) ;
    }

    std::vector<double> msec_intervals = elapsed_intervals( tictoc_stack ) ;

    // compute mean, min, max and standard deviation etc
}
Dear, thanks for the reply. I tried using your suggestions and find the min max and so on
I get a vector subscripts out of range error.
And when I try to output whats inside the vector msec_intervals it prints out 0 as there none in it.
My implementation is:

for( int i=0; i<20;i++)

If( msec_intervals[i]>max)

{

max=msec_intervals[i];

}

//And similar for min also

}

}

return 0;

}

But this is not working.
Last edited on
> I get a vector subscripts out of range error

Do not try to iterate beyond msec_intervals.size() - 1

for( std::size_t i = 0 ; i < msec_intervals.size() ; ++i ) { /* ... */ }


> And when I try to output whats inside the vector msec_intervals it prints out 0

It will print out zero if the function you are trying to time finishes executing before the clock ticks. (the clock ticks approximately CLOCKS_PER_SEC times every second).

See:
http://liveworkspace.org/code/4DZJCe$0
http://liveworkspace.org/code/4DZJCe$1
http://liveworkspace.org/code/4DZJCe$2
Thanks for the reply. I followed the links and I like what you did but because I'm using basic c++ compiler that last line that prints out the std interval gives me an error for the auto identifier.
Can that be fixed by something else? But other than that it works fine. I can't get the min ad Max unless I know what's inside the vector
> I'm using basic c++ compiler that last line that prints out the std interval
> gives me an error for the auto identifier.

Just change the loop to an old-fashioned for loop.
http://liveworkspace.org/code/4DZJCe$4
Topic archived. No new replies allowed.