Skip to content

Instantly share code, notes, and snippets.

@bitkevin
Created July 23, 2013 09:13
Show Gist options
  • Save bitkevin/6061057 to your computer and use it in GitHub Desktop.
Save bitkevin/6061057 to your computer and use it in GitHub Desktop.
#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));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment