There isn't a good standard way of getting accurate times.
Although struct timeval contains a value measured in microseconds, nothing states that the OS should update this value every microsecond. It's perfectly valid for the OS to update it once every 20mSec, by adding 20000 to it.
For non-standard ways, depends on your OS and compiler (and processor).
Win32 programmers should look at the getPerformanceCounter(sp?) function, which reads the very fast clock maintained by pentium processors.
Or for DJGPP users, how about
Code:
#include <stdio.h>
/*
* This macro found on
* http://www.c-for-dummies.com/compilers/djgpp_asm.html
*/
#define RDTSC(llptr) ({ \
__asm__ __volatile__ ( \
".byte 0x0f; .byte 0x31" \
: "=A" (llptr) \
: : "eax", "edx"); })
int main ( ) {
unsigned long long a, b;
RDTSC(a);
RDTSC(b);
printf( "%lld -> %lld = %lld\n", a, b, b-a );
return 0;
}
> long_diff = time_diff.tv_usec + (time_diff.tv_sec * 1000000);
this should be
Code:
double diff = ( end_tv.tv_sec * 1000000.0 + end_tv.tv_usec ) -
( start_tv.tv_sec * 1000000.0 + start_tv.tv_usec );