Created
July 23, 2013 09:13
-
-
Save bitkevin/6061057 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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