Timing programm with chrono problem.

So I have my program that does some stuff for a some period of time. I'm trying to output time taken at specific points of program (Starting with Time taken to execute program). The code I'm using to do that is as follows:
1
2
3
4
5
int main(int argc, char **argv) {
    std::chrono::system_clock::time_point Started=std::chrono::system_clock::now();
    //Long Stuff Here 
    std::cout << "Time taken "  << std::chrono::duration_cast<std::chrono::duration<int>>(std::chrono::system_clock::now() - Started).count() << std::endl;
}


The problem is, that outputted number does not represent the time take to execute the program.

1
2
3
4
5
6
7
8
9
time ./obe
Time taken 923469                   

real    15m23.889s
user    57m10.496s
sys     0m13.969s

echo $((923469/60))
15391


So... what am I doing wrong?
Last edited on
closed account (o3hC5Di1)
Hi there,

I'm not really familiar with this c++11 feature - but after doing a quick lookup it seems like you're handing duration_cast a parameter of type time_point, whilst this: http://cplusplus.com/reference/std/chrono/duration_cast/ states that it should take a duration as an argument.

If that doesn't seem to be the cause of the problem, perhaps it's good to try and move the question to the "general C++ programming" section as this is not strictly beginner material.

Hope that helps.

All the best,
NwN
1
2
3
4
5
6
7
8
9
10
11
12
using namespace std::chrono ;
auto start = steady_clock::now() ;

std::cin.get() ; // whatever is to be timed

auto end = steady_clock::now() ;

std::cout << duration_cast<milliseconds>(end-start).count() << " milliseconds\n"
          << duration_cast<seconds>(end-start).count() << " seconds\n" ;

using seven_ms_tick = duration< steady_clock::rep, std::ratio<7,1000> > ;
std::cout << duration_cast<seven_ms_tick>(end-start).count() << " seven millisecond ticks\n" ;

@NwN
Hmm .. I think
std::chrono::system_clock::now() - Started
returns a duration

It seems that the time is too big for time point (or duration ) to handle, as if I make the programm shorter, it works OK!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 time ./obe 
Time taken 83.469                   

real    1m23.156s
user    5m20.042s
sys     0m1.240s

//Change length of operation 

 time ./obe 
Time taken 211469                   

real    3m31.360s
user    13m22.248s
sys     0m3.286s

Topic archived. No new replies allowed.