#include #include #include void MarkTime(double *t); /* Marca o tempo entre duas chamadas 1) Para inicializar, chamar MarkTime(NULL) 2) Para finalizar, chamar MarkTime( &t ), em que t eh uma variavel double.*/ void MarkTime(double *t) { static struct timeval tstart; struct timeval tend; if (t == 0) gettimeofday(&tstart, 0); else { gettimeofday(&tend, 0); *t = (double)(tend.tv_sec - tstart.tv_sec) * 1000000; *t += tend.tv_usec - tstart.tv_usec; } } void ShowTime(double dift, char *sText); /* Exibe o intervalo de tempo na tela Usar com o resultado de MarkTime da forma: MarkTime( &t ); ShowTime(t, "Tempo de processamento: "); em que t eh uma variavel double.*/ void ShowTime(double dift, char *sText) { int64_t us = ((int64_t)dift); /* Microssegundos */ int ms = (us % 1000000LL) / 1000; int s = (us / 1000000LL) % 60; int m = (us / (60LL*1000000LL)) % 60; int h = (us / (3600LL*1000000LL)) % 24; int d = (us / (24LL*3600LL*1000000LL)); printf("%s%d dia(s) %dh %dm %ds %dms.\n",sText, d , h , m, s, ms); } int main (int argc, char **argv) { /* Testando: */ double dift = ((((7.0*24 + 3.0)*60 + 15.0)*60 + 22.0)*1000 + 123.0)*1000; ShowTime(dift, "oba oba "); return 0; }