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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define REPETITION 10
#define NOMBRE 10000000
unsigned long fact_rec_1(int n);
unsigned long fact_rec_1_plus(int n);
unsigned long fact_rec_2(int n);
unsigned long fact_rec_2_plus(int n);
unsigned long tab_min(unsigned long *tab, int taille);
unsigned long tab_max(unsigned long *tab, int taille);
float tab_moy(unsigned long *tab, int taille);
int main(void) {
unsigned long res = 0;
unsigned long fact_rec_1_res[REPETITION] = { 0 };
unsigned long fact_rec_2_res[REPETITION] = { 0 };
unsigned long fact_rec_1_plus_res[REPETITION] = { 0 };
unsigned long fact_rec_2_plus_res[REPETITION] = { 0 };
time_t begin = clock();
time_t end = clock();
int n, i;
int val = 26;
srand(time(NULL));
for (n = 0; n < REPETITION; n++) {
begin = clock();
for (i = 0; i < NOMBRE; i++) {
res = fact_rec_1_plus(rand() % val);
}
end = clock();
fact_rec_1_plus_res[n] = (end - begin) * 1000 / CLOCKS_PER_SEC;
begin = clock();
for (i = 0; i < NOMBRE; i++) {
res = fact_rec_2_plus(rand() % val);
}
end = clock();
fact_rec_2_plus_res[n] = (end - begin) * 1000 / CLOCKS_PER_SEC;
begin = clock();
for (i = 0; i < NOMBRE; i++) {
res = fact_rec_1(rand() % val);
}
end = clock();
fact_rec_1_res[n] = (end - begin) * 1000 / CLOCKS_PER_SEC;
begin = clock();
for (i = 0; i < NOMBRE; i++) {
res = fact_rec_2(rand() % val);
}
end = clock();
fact_rec_2_res[n] = (end - begin) * 1000 / CLOCKS_PER_SEC;
}
printf("fact_rec_1_min : %ld millisecondes\n",
tab_min(fact_rec_1_res, REPETITION));
printf("fact_rec_1_max : %ld millisecondes\n",
tab_max(fact_rec_1_res, REPETITION));
printf("fact_rec_1_moy : %f millisecondes\n",
tab_moy(fact_rec_1_res, REPETITION));
puts("");
printf("fact_rec_1_plus_min : %ld millisecondes\n",
tab_min(fact_rec_1_plus_res, REPETITION));
printf("fact_rec_1_plus_max : %ld millisecondes\n",
tab_max(fact_rec_1_plus_res, REPETITION));
printf("fact_rec_1_plus_moy : %f millisecondes\n",
tab_moy(fact_rec_1_plus_res, REPETITION));
puts("");
printf("fact_rec_2_min : %ld millisecondes\n",
tab_min(fact_rec_2_res, REPETITION));
printf("fact_rec_2_max : %ld millisecondes\n",
tab_max(fact_rec_2_res, REPETITION));
printf("fact_rec_2_moy : %f millisecondes\n",
tab_moy(fact_rec_2_res, REPETITION));
puts("");
printf("fact_rec_2_plus_min : %ld millisecondes\n",
tab_min(fact_rec_2_plus_res, REPETITION));
printf("fact_rec_2_plus_max : %ld millisecondes\n",
tab_max(fact_rec_2_plus_res, REPETITION));
printf("fact_rec_2_plus_moy : %f millisecondes\n",
tab_moy(fact_rec_2_plus_res, REPETITION));
return EXIT_SUCCESS;
}
unsigned long fact_rec_1(int n) {
if (n == 1)
return 1;
else
return n * fact_rec_1(n - 1);
}
unsigned long fact_rec_1_plus(int n) {
switch (n) {
case 1:
return 1;
break;
default:
return n * fact_rec_1(n - 1);
break;
}
}
unsigned long fact_rec_2(int n) {
if (n != 1)
return n * fact_rec_2(n - 1);
else
return 1;
}
unsigned long fact_rec_2_plus(int n) {
if (!(n == 1))
return n * fact_rec_2(n - 1);
else
return 1;
}
unsigned long tab_min(unsigned long *tab, int taille) {
unsigned long res = tab[0];
for (int i = 0; i < taille; i++) {
if (tab[i] < res) {
res = tab[i];
}
}
return res;
}
unsigned long tab_max(unsigned long *tab, int taille) {
unsigned long res = tab[0];
for (int i = 0; i < taille; i++) {
if (tab[i] > res) {
res = tab[i];
}
}
return res;
}
float tab_moy(unsigned long *tab, int taille) {
float res = 0;
for (int i = 0; i < taille; i++) {
res += tab[i];
}
return res / (float) taille;
} |