Wednesday, February 29, 2012

Real-Time measurement in C

For those of you, who could not find it somewhere else.
Here is the code snippet to get time measurements exact to  the microsecond on a Linux system with pure C.
Fun fact, the struct is already there, should be defined in time.h
rt_printk is real-time printf to read your output use command dmesg
 
#include 
#include 
#include 
#include 
 
int main(void)
{
 char buffer[30];
 struct timeval tv;
 time_t curtime;
 gettimeofday(&tv, NULL);
 curtime=tv.tv_sec;
 strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&curtime));
 rt_printk("%s%ld\n",buffer,tv.tv_usec);
 return 0;
}

No comments:

Post a Comment