Domanda su timeval_subtract

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
bolomanonbolo
Prode Principiante
Messaggi: 100
Iscrizione: lunedì 5 maggio 2014, 18:44
Desktop: ubuntu
Distribuzione: Ubuntu 12.04.4 LTS x86_64

Domanda su timeval_subtract

Messaggio da bolomanonbolo »

Ciao,
per calcolare il tempo di esecuzione di un programma con precisione al microsecondo ho usato la funzione timeval_subtract che si trova ad esempio in questo post: http://stackoverflow.com/questions/1468 ... lliseconds.
Non mi è chiaro però cosa succede quando il valore ritornato dalla timeval_subtract è negativo: dovrò semplicemente considerare l'opposto del risultato per ottenere il tempo che volevo calcolare?
Di seguito posto il codice così come l'ho implementato.

Codice: Seleziona tutto

#include <stdio.h>
#include <time.h>
#include <x86_64-linux-gnu/sys/time.h>
#define SCALETIME 1000000.0

/* Return 1 if the difference is negative, otherwise 0.  */
int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
{
    long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
    result->tv_sec = diff / 1000000;
    result->tv_usec = diff % 1000000;

    return (diff<0);
}


int main (int ac, char **av)
{

	struct timeval tvBeginTotal, tvEndTotal, tvElapsedTotal;
        double ElapsedTimeTotal;
	gettimeofday(&tvBeginTotal, NULL);
	//CORPO DEL CODICE
	gettimeofday(&tvEndTotal, NULL);
	timeval_subtract(&tvElapsedTotal, &tvEndTotal, &tvBeginTotal);
	ElapsedTimeTotal=tvElapsedTotal.tv_sec+ tvElapsedTotal.tv_usec/SCALETIME;
	printf("ElapsedTimeTotal: %f.\n",ElapsedTimeTotal);
	return 0;
}
Grazie.
ixamit
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 499
Iscrizione: giovedì 14 novembre 2013, 10:16

Re: Domanda su timeval_subtract

Messaggio da ixamit »

bolomanonbolo ha scritto: [...]
Non mi è chiaro però cosa succede quando il valore ritornato dalla timeval_subtract è negativo: dovrò semplicemente considerare l'opposto del risultato per ottenere il tempo che volevo calcolare?
[...]
Forse non colgo appieno il senso della domanda, ma se la funzione restituisce un valore negativo è perchè le due strutture in ingresso sono invertite oppure per overflow matematico aritmetico come da esempio:

Codice: Seleziona tutto

max@studio:~/tmp$ gcc 3.c -m32 -DBUGGY
max@studio:~/tmp$ ./a.out 
r=1
1415967078.634568 = 11-14-2014  13:11:18.634568
1415970079.634680 = 11-14-2014  14:01:19.634680
-1293.-967184 = 01-01-1970  00:38:27.-967184
ElapsedTimeTotal: -1293.967184.
max@studio:~/tmp$ 
il sorgente:

Codice: Seleziona tutto

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#define SCALETIME 1000000.0


void timeval_print(struct timeval *tv)
{
    char buffer[30];
    time_t curtime;

    printf("%ld.%06ld", tv->tv_sec, tv->tv_usec);
    curtime = tv->tv_sec;
    strftime(buffer, 30, "%m-%d-%Y  %T", localtime(&curtime));
    printf(" = %s.%06ld\n", buffer, tv->tv_usec);
}


/* Return 1 if the difference is negative, otherwise 0.  */
int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
{
    long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
    result->tv_sec = diff / 1000000;
    result->tv_usec = diff % 1000000;

    return (diff<0);
}


int main (int ac, char **av)
{

    struct timeval tvBeginTotal, tvEndTotal, tvElapsedTotal;
    double ElapsedTimeTotal;

    gettimeofday(&tvBeginTotal, NULL);

    //CORPO DEL CODICE
    sleep(1);
 
    gettimeofday(&tvEndTotal, NULL);

    int r=0;
#ifdef BUGGY
    while ((r=timeval_subtract(&tvElapsedTotal, &tvEndTotal, &tvBeginTotal))==0)
        tvEndTotal.tv_sec+=1000;
#else
    r=timeval_subtract(&tvElapsedTotal, &tvEndTotal, &tvBeginTotal); 
#endif

edit : errata sul tipo overflow
Scrivi risposta

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: 0 utenti iscritti e 17 ospiti