se sei in windows potresti usare l'api QueryPerformanceFrequency, in ambiente *nix le funzioni in sys/time.h.
Io ho testato solo in windows e funziona, ecco il codice.
codice:
#ifdef _WIN32
#include <windows.h>
static LARGE_INTEGER _tstart, _tend;
static LARGE_INTEGER freq;
void tstart(void)
{
static int first = 1;
if(first) {
QueryPerformanceFrequency(&freq);
first = 0;
}
QueryPerformanceCounter(&_tstart);
}
void tend(void)
{
QueryPerformanceCounter(&_tend);
}
double tval()
{
return ((double)_tend.QuadPart -
(double)_tstart.QuadPart)/((double)freq.QuadPart);
}
#else
#include <sys/time.h>
static struct timeval _tstart, _tend;
static struct timezone tz;
void tstart(void)
{
gettimeofday(&_tstart, &tz);
}
void tend(void)
{
gettimeofday(&_tend,&tz);
}
double tval()
{
typedef signed long s;
s t1, t2;
t1 = (s)_tstart.tv_sec + (s)_tstart.tv_usec/(1000*1000);
t2 = (s)_tend.tv_sec + (s)_tend.tv_usec/(1000*1000);
return t2-t1;
}
#endif
all'inizio dell'evento chiami tstart(), alla fine tend(), e tval() restiturà il tempo trascorso.