Hi,
even though it's an old post, I thought I'd post some code I find quite useful for measuring runtime of a process, since it might be useful to someone else.
#include <sys/time.h>
#include <sys/resource.h>
typedef struct tag_time_measure
{
struct timeval startTimeVal;
struct timeval stopTimeVal;
struct rusage startTimeUsage;
struct rusage stopTimeUsage;
} time_measure;
time_measure * startTimeMeasuring()
{
time_measure * tu = malloc(sizeof(time_measure));
if(!tu)
exit(1);
getrusage(RUSAGE_SELF, &tu->startTimeUsage);
gettimeofday(&tu->startTimeVal,0);
return tu;
}
void stopTimeMeasuring(time_measure * tu)
{
getrusage(RUSAGE_SELF, &tu->stopTimeUsage);
gettimeofday(&tu->stopTimeVal,0);
}
void printMeasuredTime(time_measure * tu)
{
struct timeval elapsedVal;
struct timeval userVal;
struct timeval systemVal;
double elapsed_millis = 0.0f;
double user_millis = 0.0f;
double system_millis = 0.0f;
timersub(&tu->stopTimeVal, &tu->startTimeVal, &elapsedVal);
timersub(&tu->stopTimeUsage.ru_utime, &tu->startTimeUsage.ru_utime, &userVal);
timersub(&tu->stopTimeUsage.ru_stime, &tu->startTimeUsage.ru_stime, &systemVal);
elapsed_millis = elapsedVal.tv_sec * 1000 + (double) elapsedVal.tv_usec / 1000;
user_millis = userVal.tv_sec * 1000 + (double) userVal.tv_usec / 1000;
system_millis = systemVal.tv_sec * 1000 + (double) systemVal.tv_usec / 1000;
printf("\n\n---Program execution times in milliseconds--- \n");
printf("Total:\t\t %f\nUser:\t\t %f\nSystem:\t\t %f\n", elapsed_millis, user_millis, system_millis);
}
Then one could use the functions like:
int main(void)
{
time_measure * tu = startTimeMeasuring();
doSomethingExpensiveHere();
stopTimeMeasuring(tu);
printMeasuredTime(tu);
free(tu);
return EXIT_SUCCESS;
}
Code can easily be extended to take advantage of the other neat stuff rusage offers (see http://www.gnu.org/s/libc/manual/html_node/Resource-Usage.html for more details). Hope someone will find that useful :)
Regards,
Vassil