Timer

#include <string.h>
#include <stdio.h>
#include <time.h>
#include <sys/resource.h>
 
/* Saved resource information for the beginning of an operation */
static struct rusage sBegin;
 
/*
 ** Begin timing an operation
 */
static void beginTimer(void){
  getrusage(RUSAGE_SELF, &sBegin);
}
 
/* Return the difference of two time_structs in seconds */
static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
  (double)(pEnd->tv_sec - pStart->tv_sec);
}
 
/*
 ** Print the timing results.
 */
static void endTimer(void){
  struct rusage sEnd;
  getrusage(RUSAGE_SELF, &sEnd);
  printf("CPU Time: user %f sys %f\n",
         timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
         timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
}
c/timer.txt · 最后更改: 2012/01/26 22:38 由 admin
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki