Im very new to C++ and I was wondering how to go about implementing a simple timer which keeps track of how much time has passed while the program is running. eg how will i know when 300 seconds has passed?
Asked
Active
Viewed 1,531 times
0
-
3Are you trying to benchmark how fast a program ran, or are you looking for it to run something after a certain amount of time? – kmort Feb 06 '14 at 01:09
2 Answers
1
#include <time.h>
clock_t t1,t2;
t1 = clock();
//Your code here
t2 = clock();
//Time taken for running your code segment
double time_dif = (double)(t2 - t1)/CLOCKS_PER_SEC;
Actually t1-t2 gives you the number of total clock cycles during your execution, so divide that by CLOCKS_PER_SEC to get the actual time

Varo
- 831
- 1
- 7
- 16