0

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?

noobcoder
  • 6,089
  • 2
  • 18
  • 34
  • 3
    Are 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 Answers2

1

gettimeofday is one of easiest way. Basic idea is described here:

http://www.cplusplus.com/forum/unices/50561/

wiesniak
  • 558
  • 2
  • 11
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