1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#ifdef __linux
#include <unistd.h>
#endif
/*
* Recuperateur de temps :
*/
double GetClock ( void )
{
double d=-1.0 ;
#ifndef __linux
#ifdef _WIN32
struct timeval tval ;
struct timezone *tz=(struct timezone *)NULL ;
timerclear(&tval);
if ( gettimeofday(&tval, tz) )
{
#ifdef VERBOSE
fprintf (stderr, "\nCLOCK ERROR !!!\n");
#endif
}
else
{
d = ((double)(tval.tv_usec)/1000000.0) ;
d = (double) tval.tv_sec + d ;
}
#else
struct timespec cur_time, res ;
if (clock_gettime(CLOCK_REALTIME, &cur_time))
{
#ifdef VERBOSE
fprintf (stderr, "\nCLOCK ERROR !!!\n");
#endif
}
else
{
d = ((double)(cur_time.tv_nsec)/1000.0) / 1000000.0 ;
d = (double) cur_time.tv_sec + d ;
}
#endif
#else
struct timeval tval ;
struct timezone *tz=(struct timezone *)NULL ;
timerclear(&tval);
if ( gettimeofday(&tval, tz) )
{
#ifdef VERBOSE
fprintf (stderr, "\nCLOCK ERROR !!!\n");
#endif
}
else
{
d = ((double)(tval.tv_usec)/1000000.0) ;
d = (double) tval.tv_sec + d ;
}
#endif
return d ;
}
/*
* Programme de test:
*/
int main( void )
{
int i ;
double t_depart, t_fin, t ;
double duree_en_millisecondes ; /* 1000 pour une seconde, 3600000 pour une heure */
/* Calcul temps de depart */
t_depart = GetClock() ;
printf("t_depart = %d\n",t_depart);
t = 0.0 ;
for ( i = 0 ; i < 1 ; i++ )
t = t + 1.0 ;
t_fin = GetClock() ;
if ( (t_depart*t_fin) < 0.0 )
fprintf ( stderr, "\n ATTENTION !!! Une erreur s'est produite\n");
else
fprintf ( stderr, "\n Temps écoulé %g secondes pour une boucle 1 : \n",(t_fin - t_depart));
t_depart = t_fin ;
t = 0.0 ;
for ( i = 0 ; i < 5000 ; i++ )
t = t + 1.0 ;
t_fin= GetClock() ;
if ( (t_depart*t_fin) < 0.0 )
fprintf ( stderr, "\n ATTENTION !!! Une erreur s'est produite\n");
else
fprintf ( stderr, "\n Temps écoulé %g secondes pour une boucle 10 : \n",(t_fin - t_depart));
t_depart = GetClock() ;
//printf("t_depart = %f\n",t_depart);
t_fin = GetClock() ;
//printf("t_fin = %f\n",t_fin);
fprintf ( stderr, "\n Résolution maximum %f secondes : \n",(t_fin - t_depart));
return EXIT_SUCCESS ;
} |
Partager