I'm attempting to monitor system time elapsed across multiple applications and data paths via gettimeoday and localtime. In this example, I want to grab the system time (microsecond percision) right before my code runs. I'm currently doing it like this:
#include <stdio.h>
#include <sys/time.h>
struct timeval tv;
struct timezone tz;
struct tm *tm;
**code I don't care about**
gettimeofday(&tv, NULL);
tm=localtime(&tv.tv_sec);
**code to watch**
printf(" %d:%02d:%02d %ld \n", tm->tm_hour, tm->tm_min,
tm->tm_sec, tv.tv_usec);
From what I understand the localtime call definitely incurs a bit of overhead and can throw off accuracy. I might be totally wrong on this, but should I wait to call localtime until after my code to watch completes? I'm assuming that localtime is just doing a semi-expensive conversion of gettimeofday's results, and thus it should be placed right before the printf statement.