Next Spaceship

Driving into future…

How to Record Time in C++?

| Comments

To record the time your program costs, you can use the function clock(), that is defined in <ctime>. Here is an example showing how to use this function:

How To Record Time
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <ctime>
using namespace std;
int main() {
  clock_t t1, t2;
  t1 = clock();
  int i, s = 0;
  for (i = 0; i < 100000000; i++)
      s += i;
  t2 = clock();
  printf("%fn", (double)(t2 - t1) / CLOCKS_PER_SEC);
  return 0;
}

Comments