Bonjour,
Je dois mesurer le temps d'execution d'une fonction, j'utilise donc pour cela gettimeofday. Cependant, il arrive que le deuxieme appel gettimeofday retourne une valeur inférieur à celle renvoyé par le premier appel.
Voici le code de la fonction qui réalise la mesure :
Celle ci est appellé à partir du main suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* ** File : mesureAlgo.c ** Author : kal ** Created : Sun Oct 29 16:31:41 2006 */ #ifdef __cplusplus #error This source file is not C++ but rather C. Please use a C-compiler #endif #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <sys/time.h> #include <time.h> #include "mesureAlgo.h" #include "couple.h" /* macros ============================================================== */ /* constants =========================================================== */ /* types =============================================================== */ /* structures ========================================================== */ /* private variables =================================================== */ /* private functions =================================================== */ /* internal public functions =========================================== */ /* entry points ======================================================== */ long int mesureAlgo(couple (* fonction) (int tab[], int i, int j), int tab[], int i, int j, int nbIter) { struct timeval *tmDebut = (struct timeval *) malloc(sizeof(struct timeval)); struct timeval *tmFin = (struct timeval *) malloc(sizeof(struct timeval)); long int temps; assert(gettimeofday(tmDebut, NULL) == 0); for (int x = 0 ; x < nbIter ; x++) { (* fonction) (tab, i, j); } assert(gettimeofday(tmFin, NULL) == 0); temps = (tmFin->tv_usec - tmDebut->tv_usec) / (double) nbIter; printf("debut = %ld ; fin = %ld ; temps = %ld us\n", tmDebut->tv_usec, tmFin->tv_usec, temps); free(tmFin); free(tmDebut); return temps; } /* public variables ==================================================== */
Je ne vous copie pas le code de la fonction generate qui s'occupe de placer des 1 et de 0 dans un tableau, sauf si vous en avez besoin.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* ** File : creerPoints.c ** Author : kal ** Created : Sun Oct 29 18:14:06 2006 */ #ifdef __cplusplus #error This source file is not C++ but rather C. Please use a C-compiler #endif #include <stdio.h> #include <stdlib.h> #include <time.h> #include "mesureAlgo.h" #include "generate.h" #include "iter.h" #include "recursif_1.h" /* macros ============================================================== */ /* constants =========================================================== */ /* types =============================================================== */ /* structures ========================================================== */ /* private variables =================================================== */ /* private functions =================================================== */ /* internal public functions =========================================== */ void usage(const char *arg0) { fprintf(stderr, "Usage : %s <nbIter>\n", arg0); exit(EXIT_FAILURE); } /* entry points ======================================================== */ int main(int argc, char **argv) { if (argc != 2) { usage(argv[0]); } long int nbIter = strtol (argv[1], NULL, 10); int nbZero; int instance[] = {10, 50, 100, 500, 1000, 2500, 5000, 10000, 15000, 200000}; srand(time(NULL)); for (unsigned int i = 0 ; i < sizeof(instance) / sizeof (int) ; i++ ) { int tab[instance[i]]; nbZero = 1 + (int) (instance[i] * (rand() / (RAND_MAX + 1.0))); generate(tab, instance[i], nbZero); printf("I1: %d %ld\n\n", instance[i], mesureAlgo(rechIter, tab, 0, instance[i] - 1, nbIter)); printf("R1: %d %ld\n\n", instance[i], mesureAlgo(rechRec1, tab, 0, instance[i] - 1, nbIter)); } return EXIT_SUCCESS; } /* public variables ==================================================== */
Voici un exemple d'execution :
Je vous laisse constater le dernier résultat de l'exécution...kal@kal-desktop ~/ALGO $ ./creerPoints 5
debut = 560453 ; fin = 560455 ; temps = 0 us
I1: 10 0
debut = 560590 ; fin = 560615 ; temps = 5 us
R1: 10 5
debut = 560668 ; fin = 560672 ; temps = 0 us
I1: 50 0
debut = 560716 ; fin = 560844 ; temps = 25 us
R1: 50 25
debut = 560906 ; fin = 560911 ; temps = 1 us
I1: 100 1
debut = 560955 ; fin = 561198 ; temps = 48 us
R1: 100 48
debut = 561291 ; fin = 561315 ; temps = 4 us
I1: 500 4
debut = 561360 ; fin = 562542 ; temps = 236 us
R1: 500 236
debut = 562684 ; fin = 562727 ; temps = 8 us
I1: 1000 8
debut = 562772 ; fin = 565198 ; temps = 485 us
R1: 1000 485
debut = 565394 ; fin = 565539 ; temps = 29 us
I1: 2500 29
debut = 565588 ; fin = 572534 ; temps = 1389 us
R1: 2500 1389
debut = 572855 ; fin = 573146 ; temps = 58 us
I1: 5000 58
debut = 573193 ; fin = 665674 ; temps = 18496 us
R1: 5000 18496
debut = 666523 ; fin = 667049 ; temps = 105 us
I1: 10000 105
debut = 667098 ; fin = 734231 ; temps = 13426 us
R1: 10000 13426
debut = 735126 ; fin = 735949 ; temps = 164 us
I1: 15000 164
debut = 735997 ; fin = 815849 ; temps = 15970 us
R1: 15000 15970
debut = 831513 ; fin = 881612 ; temps = 10019 us
I1: 200000 10019
debut = 881731 ; fin = 466671 ; temps = -83012 us
R1: 200000 -83012![]()
Si quelqu'un a une idée, merci d'avance.
Partager