#include <time.h>
#include <stdio.h>
#include <sys/time.h>
int main(int argc,char **argv) {     
  clock_t start, end;
  double cpu_time_used;
  struct timeval h,h2;
  struct timezone tz;
  int max=1000,acum=0,i,j;
  gettimeofday(&h,&tz);
  start = clock();
  for (i=0;i<max;i++) {
    acum+=1;
    for (j=0;j<max;j++) {
      acum=0;
    }
  }
  end = clock(); 
  gettimeofday(&h2,&tz);
  cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
  printf("%f %d %f %d %d %d",cpu_time_used,CLOCKS_PER_SEC,cpu_time_used,h.tv_usec,h2.tv_usec,h2.tv_usec-h.tv_usec);
}

Para medir en milésimas de segundos, el campo tv.usec de la estructura timeval.

ProgramarEnC/MedirTiempo (last edited 2009-07-15 15:27:03 by localhost)