CPP (C++) time

October 21st, 2011 by xrigher Leave a reply »

There are may ways to estimate how long a program (or a part of the program) elapsed.

Use clock()

clock() is a standard function defined in . Here is the usage:

#include <ctime>

int main() {
  clock_t start, end;
  start = clock();
  // do sth...
  end = clock();

  double elapsedSeconds = (end - start) / CLOCKS_PER_SEC;
}
 

Use gettimeofday()

clock() is a linux function defined in . Here is the usage: #include <sys/time.h> int main() { timeval start, end; gettimeofday(&start, 0); // the second parameter is the timezone. // do sth... gettimeofday(&end, 0); double elapsedSeconds = (end.tv_sec - start.tv_sec + 1e-6*(end.tv_usec - start.tv_usec)); }

Advertisement

1 comment

  1. Annoying_Orange says:

    Why Program 1 gives me out put 0.00000 no matter what?

    Does time ticks cannot be store on 32-bit system?? :S

Leave a Reply