title: Elapsed time of running a C program
body: <p>I would like to know what lines of C code to add to a program so that it tells me the total time that the program takes to run. I guess there should be counter initialization near the beginning of main and one after the main function ends. Is the right header <code>clock.h</code>?</p>
  
  <p>Thanks a lot...</p>
  
  <p><strong>Update</strong> I have a Win Xp machine. Is it just adding <code>clock()</code> at the beginning and another <code>clock()</code> at the end of the program? Then I can estimate the time difference. Yes, you're right it's <code>time.h</code>.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;share.h&gt;
  #include &lt;time.h&gt;
  
  
  void f(long double fb[], long double fA, long double fB);
  
  int main() {
  
  clock_t start, end;
  start = clock();
  
  
  const int ARRAY_SIZE = 11;
  
  long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
  
  int i;
  long double A, B;
  
  if (z == NULL) {
      printf("Out of memory\n");
      exit(-1);
  }
  
  A = 0.5;
  B = 2;
  
  
  for (i = 0; i &lt; ARRAY_SIZE; i++) {
      z[i] = 0;
  }
  
  z[1] = 5;
  
  f(z, A, B);
  
  for (i = 0; i &lt; ARRAY_SIZE; i++)
      printf("z is %.16Le\n", z[i]);
  
  
  
  free(z);
  z = NULL;
  
  end = clock();
  printf("Took %ld ticks\n", end-start);
  printf("Took %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);
  
  
  
  return 0;  
  }  
  
  void f(long double fb[], long double fA, long double fB) {
      fb[0] = fb[1]* fA;
      fb[1] = fb[1] - 1;
      return;
   }  
  </code></pre>
  
  <p>Some errors with MVS2008:</p>
  
  <pre>
  testim.c(16) : error C2143: syntax error : missing ';' before 'const'  
  testim.c(18) :error C2143: syntax error : missing ';' before 'type'  
  testim.c(20) :error C2143: syntax error : missing ';' before 'type'   
  testim.c(21) :error C2143: syntax error : missing ';' before 'type'    
  testim.c(23) :error C2065: 'z' : undeclared identifier   
  testim.c(23) :warning C4047: '==' : 'int' differs in levels of indirection from 'void *'  
  testim.c(28) : error C2065: 'A' : undeclared identifier
  testim.c(28) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data   
  </pre>
  
  <p>and it goes to 28 errors. Note that I don't have any errors/warnings without your clock codes.</p>
  
  <p><strong>LATEST NEWS:</strong> I unfortunately didn't get a good reply here. But after a search on Google, the code is working. Here it is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;time.h&gt;
  
  
  void f(long double fb[], long double fA);
  
  int main() {
  
  clock_t start = clock();
  
  
  const int ARRAY_SIZE = 11;
  
  long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
  
  int i;
  long double A;
  
  if (z == NULL) {
  printf("Out of memory\n");
  exit(-1);
  }
  
  A = 0.5;
  
  
  for (i = 0; i &lt; ARRAY_SIZE; i++) {
  z[i] = 0;
  }
  
  z[1] = 5;
  
  f(z, A);
  
  for (i = 0; i &lt; ARRAY_SIZE; i++)
  printf("z is %.16Le\n", z[i]);
  
  
  
  free(z);
  z = NULL;
  
  printf("Took %f seconds\n", ((double)clock()-start)/CLOCKS_PER_SEC);
  
  
  
  return 0;
  }
  
  void f(long double fb[], long double fA) {
  fb[0] = fb[1]* fA;
  fb[1] = fb[1] - 1;
  return;
  }
  </code></pre>
  
  <p>Cheers</p>
  
  <p>Update on April 10: Here's a better solution thanks to "JustJeff"</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void f(long double fb[], long double fA);
  
  const int ARRAY_SIZE = 11;
  
  int main(void)
  {
  
     long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
     int i;
     long double A;
  
     LARGE_INTEGER freq;
     LARGE_INTEGER t0, tF, tDiff;
     double elapsedTime;
     double resolution;
  
     if (z == NULL) {
     printf("Out of memory\n");
     exit(-1);
     }
     QueryPerformanceFrequency(&amp;freq);
     QueryPerformanceCounter(&amp;t0);
     // code to be timed goes HERE
     {
      A = 0.5;
  
  
      for (i = 0; i &lt; ARRAY_SIZE; i++) {
      z[i] = 0;
      }
  
      z[1] = 5;
      f(z, A);
  
  
      for (i = 0; i &lt; ARRAY_SIZE; i++)
      printf("z is %.16Le\n", z[i]);
  
      free(z);
      z = NULL;
  
     }
  QueryPerformanceCounter(&amp;tF);
  tDiff.QuadPart = tF.QuadPart - t0.QuadPart;
  elapsedTime = tDiff.QuadPart / (double) freq.QuadPart;
  resolution = 1.0 / (double) freq.QuadPart;
  printf("Your performance counter ticks %I64u times per second\n", freq.QuadPart);
  printf("Resolution is %lf nanoseconds\n", resolution*1e9);
  printf("Code under test took %lf sec\n", elapsedTime);
  return 0;
  }
  
  
  void f(long double fb[], long double fA) {
  fb[0] = fb[1]* fA;
  fb[1] = fb[1] - 1;
  return;
  }
  </code></pre>
  
  <p>It works both with MVS2008 and with Borland C++ builderX from 2003.</p>