10 Commits

Author SHA1 Message Date
Karl Framery cbc0cf780e EXO 4 - optimisation F7 2026-10-21 12:20:55 +02:00
Karl Framery 64e3432b33 EXO 4 - optimisation F5 2026-10-21 11:58:37 +02:00
Karl Framery 82a77ec17c EXO 4 - optimisation F3 2026-10-21 11:32:06 +02:00
Karl Framery bf16afd727 EXO 4 - optimisation F3 2026-10-21 11:30:59 +02:00
Karl Framery 96abb3e2d0 EXO 4 - optimisation F3 2026-10-21 11:30:16 +02:00
Ozvann Abraham 97155f2688 Merge branch 'main' into main 2026-10-21 11:24:55 +02:00
abraham 1e3ac9d8d2 fonction 4 : O(n + J) 2026-10-21 11:19:08 +02:00
Karl Framery a5e748e993 EXO 4 - optimisation F1 2026-10-21 11:17:43 +02:00
abraham df4e8876fb fonction 2 : O(n + C) 2026-10-21 11:10:03 +02:00
abraham 1f17823a2c Palier 1 : objectifs 2026-10-21 10:59:57 +02:00
7 changed files with 6409 additions and 26 deletions
+108
View File
@@ -0,0 +1,108 @@
# Groupe :
- Karl FRAMERY
- Ozvann ABRAHAM
# 1.Mesurer
```
gcc -O2 banque.c -o banque && time ./banque
```
```
Empreinte : ff68d260a7a58441
real 0m4,793s
user 0m4,772s
sys 0m0,005s
```
Le temps d'éxecution du programme est de 4.793 seconde et l'emprunte est : [ff68d260a7a58441]
# 2.Profiler
La fonction F1 : <trier_par_montant> est d'une complexité <span style="color:yellow">[O(n²)]</span> pour une utilisation du temps de 43.77% du temps totale d'exécution pour 1 call.
La fonction F7 : <nb_paires_opposees> est d'une complexité <span style="color:yellow">[O(n²)]</span> pour une utilisation du temps de 15.32% du temps totale d'exécution pour 1 call.
La fonction F8 : <nb_facons_rendre> est d'une complexité <span style="color:red">[O(2^n)]</span> pour une utilisation du temps de 12.84% du temps totale d'exécution pour 1 call.
La fonction F6 : <rechercher_par_id> est d'une complexité <span style="color:blue">[O(n)]</span> pour une utilisation du temps de 12.21% du temps totale d'exécution pour 30000 calls.
On peut voir que les fonctions les plus coûteuse ont les plus grande complexité.
# 3.Calculer
### Fonction F1
La complexité de la fonction est de <span style="color:yellow">[O(n²)]</span> pour un M de 6
### Fonction F2
La complexité de la fonction est de <span style="color:yellow">[O(n²)]</span> pour un M de 5
### Fonction F3
La complexité de la fonction est de <span style="color:yellow">[O(n²)]</span> pour un M de 4
### Fonction F4
La complexité de la fonction est de <span style="color:yellow">[O(n²)]</span> pour un M de 4
### Fonction F5
La complexité de la fonction est de <span style="color:yellow">[O(n³)]</span> pour un M de 5
### Fonction F6
La complexité de la fonction est de <span style="color:blue">[O(n)]</span> pour un M de 3
### Fonction F7
La complexité de la fonction est de <span style="color:yellow">[O(n²)]</span> pour un M de 4
### Fonction F8
La complexité de la fonction est de <span style="color:red">[O(2^n)]</span> pour un M de 4
### Fonction F9
La complexité de la fonction est de <span style="color:blue">[O(1)]</span> pour un M de 14
### Fonction F10
La complexité de la fonction est de <span style="color:blue">[O(n)]</span> pour un M de 2
# 4. Optimisation
- Optimisation de la fonction 1 nom du commit : ""
- Optimisation de la fonction 2 nom du commit : "fonction 2 : O(n + C)". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 3 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 4 nom du commit : "fonction 4 : O(n + J)". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 5 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 6 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 7 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 8 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 9 nom du commit : "". Emprunte : ff68d260a7a58441
- Optimisation de la fonction 10 nom du commit : "". Emprunte : ff68d260a7a58441
Executable
BIN
View File
Binary file not shown.
+64 -26
View File
@@ -73,15 +73,15 @@ Transaction *generer_transactions(int n)
/* F1 : trie les transactions par montant croissant, puis par id */
void trier_par_montant(Transaction *t, int n)
{
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (t[j].montant > t[j + 1].montant ||
(t[j].montant == t[j + 1].montant && t[j].id > t[j + 1].id)) {
Transaction tmp = t[j];
t[j] = t[j + 1];
t[j + 1] = tmp;
}
for (int i = 1; i < n; i++) {
Transaction cle = t[i];
int j = i - 1;
while (j >= 0 && (t[j].montant > cle.montant ||
(t[j].montant == cle.montant && t[j].id > cle.id))) {
t[j + 1] = t[j];
j--;
}
t[j + 1] = cle;
}
}
@@ -103,14 +103,14 @@ int nb_clients_distincts(const Transaction *t, int n)
return nb;
}
/* F3 : solde total de chaque client */
/* F3 : solde total de chaque client en O(n + c) */
void total_par_client(const Transaction *t, int n, long *totaux)
{
for (int c = 0; c < NB_CLIENTS; c++) {
totaux[c] = 0;
for (int i = 0; i < n; i++)
if (t[i].client == c)
totaux[c] += t[i].montant;
}
for (int i = 0; i < n; i++) {
totaux[t[i].client] += t[i].montant;
}
}
@@ -125,24 +125,30 @@ void total_par_jour(const Transaction *t, int n, long *totaux)
}
}
/* F5 : période de jours consécutifs dont la somme des soldes est maximale */
/* F5 : période de jours consécutifs dont la somme des soldes est maximale en O(J) */
long meilleure_periode(const long *totaux, int nb_jours, int *debut, int *fin)
{
long meilleur = totaux[0];
*debut = 0;
*fin = 0;
for (int i = 0; i < nb_jours; i++) {
for (int j = i; j < nb_jours; j++) {
long somme = 0;
for (int k = i; k <= j; k++)
somme += totaux[k];
if (somme > meilleur) {
meilleur = somme;
*debut = i;
*fin = j;
}
long somme_courante = totaux[0];
int debut_courant = 0;
for (int j = 1; j < nb_jours; j++) {
if (somme_courante < 0) {
somme_courante = totaux[j];
debut_courant = j;
} else {
somme_courante += totaux[j];
}
if (somme_courante > meilleur) {
meilleur = somme_courante;
*debut = debut_courant;
*fin = j;
}
}
return meilleur;
}
@@ -156,13 +162,45 @@ const Transaction *rechercher_par_id(const Transaction *t, int n, int id)
}
/* F7 : nombre de paires de transactions de montants opposés (a + b = 0) */
static int cmp_long(const void *a, const void *b)
{
long x = *(const long *)a;
long y = *(const long *)b;
return (x > y) - (x < y);
}
long nb_paires_opposees(const Transaction *t, int n)
{
/* 1. Copier et trier les montants : O(n log n) */
long *m = malloc(n * sizeof(long));
for (int i = 0; i < n; i++) {
m[i] = t[i].montant;
}
qsort(m, n, sizeof(long), cmp_long);
/* 2. Deux pointeurs : O(n) au lieu de O(n²) */
long nb = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (t[i].montant + t[j].montant == 0)
int g = 0, d = n - 1;
while (g < d) {
long somme = m[g] + m[d];
if (somme < 0) {
g++;
} else if (somme > 0) {
d--;
} else {
/* somme == 0 : on compte tous les m[d] identiques en face de m[g] */
for (int k = d; k > g && m[k] == m[d]; k--) {
nb++;
}
g++; /* Puis on passe simplement au m[g] suivant ! */
}
}
free(m);
return nb;
}
Executable
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+220
View File
@@ -0,0 +1,220 @@
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
43.77 5.20 5.20 1 5.20 5.20 trier_par_montant
15.32 7.02 1.82 1 1.82 1.82 nb_paires_opposees
12.84 8.54 1.52 1 1.52 1.52 nb_facons_rendre
12.21 9.99 1.45 30000 0.00 0.00 rechercher_par_id
6.06 10.71 0.72 6017 0.00 0.00 longueur
4.04 11.20 0.48 1 0.48 0.48 meilleure_periode
1.94 11.43 0.23 1 0.23 0.23 nb_clients_distincts
1.60 11.62 0.19 1 0.19 0.19 total_par_client
1.01 11.73 0.12 _init
0.84 11.84 0.10 1 0.10 0.10 total_par_jour
0.38 11.88 0.04 30000 0.00 0.00 categorie
0.00 11.88 0.00 149998 0.00 0.00 alea
0.00 11.88 0.00 6017 0.00 0.00 ajouter_ligne
0.00 11.88 0.00 1 0.00 0.00 empreinte
0.00 11.88 0.00 1 0.00 0.00 generer_transactions
% the percentage of the total running time of the
time program used by this function.
cumulative a running sum of the number of seconds accounted
seconds for by this function and those listed above it.
self the number of seconds accounted for by this
seconds function alone. This is the major sort for this
listing.
calls the number of times this function was invoked, if
this function is profiled, else blank.
self the average number of milliseconds spent in this
ms/call function per call, if this function is profiled,
else blank.
total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.
name the name of the function. This is the minor sort
for this listing. The index shows the location of
the function in the gprof listing. If the index is
in parenthesis it shows where it would appear in
the gprof listing if it were to be printed.
Copyright (C) 2012-2026 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
Call graph (explanation follows)
granularity: each sample hit covers 4 byte(s) for 0.08% of 11.88 seconds
index % time self children called name
<spontaneous>
[1] 99.0 0.00 11.76 main [1]
5.20 0.00 1/1 trier_par_montant [2]
1.82 0.00 1/1 nb_paires_opposees [3]
1.52 0.00 1/1 nb_facons_rendre [4]
1.45 0.00 30000/30000 rechercher_par_id [5]
0.00 0.72 6017/6017 ajouter_ligne [6]
0.48 0.00 1/1 meilleure_periode [8]
0.23 0.00 1/1 nb_clients_distincts [9]
0.19 0.00 1/1 total_par_client [10]
0.10 0.00 1/1 total_par_jour [12]
0.04 0.00 30000/30000 categorie [13]
0.00 0.00 1/1 generer_transactions [16]
0.00 0.00 1/1 empreinte [15]
-----------------------------------------------
5.20 0.00 1/1 main [1]
[2] 43.8 5.20 0.00 1 trier_par_montant [2]
-----------------------------------------------
1.82 0.00 1/1 main [1]
[3] 15.3 1.82 0.00 1 nb_paires_opposees [3]
-----------------------------------------------
266723738 nb_facons_rendre [4]
1.52 0.00 1/1 main [1]
[4] 12.8 1.52 0.00 1+266723738 nb_facons_rendre [4]
266723738 nb_facons_rendre [4]
-----------------------------------------------
1.45 0.00 30000/30000 main [1]
[5] 12.2 1.45 0.00 30000 rechercher_par_id [5]
-----------------------------------------------
0.00 0.72 6017/6017 main [1]
[6] 6.1 0.00 0.72 6017 ajouter_ligne [6]
0.72 0.00 6017/6017 longueur [7]
-----------------------------------------------
0.72 0.00 6017/6017 ajouter_ligne [6]
[7] 6.1 0.72 0.00 6017 longueur [7]
-----------------------------------------------
0.48 0.00 1/1 main [1]
[8] 4.0 0.48 0.00 1 meilleure_periode [8]
-----------------------------------------------
0.23 0.00 1/1 main [1]
[9] 1.9 0.23 0.00 1 nb_clients_distincts [9]
-----------------------------------------------
0.19 0.00 1/1 main [1]
[10] 1.6 0.19 0.00 1 total_par_client [10]
-----------------------------------------------
<spontaneous>
[11] 1.0 0.12 0.00 _init [11]
-----------------------------------------------
0.10 0.00 1/1 main [1]
[12] 0.8 0.10 0.00 1 total_par_jour [12]
-----------------------------------------------
0.04 0.00 30000/30000 main [1]
[13] 0.4 0.04 0.00 30000 categorie [13]
-----------------------------------------------
0.00 0.00 149998/149998 generer_transactions [16]
[14] 0.0 0.00 0.00 149998 alea [14]
-----------------------------------------------
0.00 0.00 1/1 main [1]
[15] 0.0 0.00 0.00 1 empreinte [15]
-----------------------------------------------
0.00 0.00 1/1 main [1]
[16] 0.0 0.00 0.00 1 generer_transactions [16]
0.00 0.00 149998/149998 alea [14]
-----------------------------------------------
This table describes the call tree of the program, and was sorted by
the total amount of time spent in each function and its children.
Each entry in this table consists of several lines. The line with the
index number at the left hand margin lists the current function.
The lines above it list the functions that called this function,
and the lines below it list the functions this one called.
This line lists:
index A unique number given to each element of the table.
Index numbers are sorted numerically.
The index number is printed next to every function name so
it is easier to look up where the function is in the table.
% time This is the percentage of the `total' time that was spent
in this function and its children. Note that due to
different viewpoints, functions excluded by options, etc,
these numbers will NOT add up to 100%.
self This is the total amount of time spent in this function.
children This is the total amount of time propagated into this
function by its children.
called This is the number of times the function was called.
If the function called itself recursively, the number
only includes non-recursive calls, and is followed by
a `+' and the number of recursive calls.
name The name of the current function. The index number is
printed after it. If the function is a member of a
cycle, the cycle number is printed between the
function's name and the index number.
For the function's parents, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the function into this parent.
children This is the amount of time that was propagated from
the function's children into this parent.
called This is the number of times this parent called the
function `/' the total number of times the function
was called. Recursive calls to the function are not
included in the number after the `/'.
name This is the name of the parent. The parent's index
number is printed after it. If the parent is a
member of a cycle, the cycle number is printed between
the name and the index number.
If the parents of the function cannot be determined, the word
`<spontaneous>' is printed in the `name' field, and all the other
fields are blank.
For the function's children, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the child into the function.
children This is the amount of time that was propagated from the
child's children to the function.
called This is the number of times the function called
this child `/' the total number of times the child
was called. Recursive calls by the child are not
listed in the number after the `/'.
name This is the name of the child. The child's index
number is printed after it. If the child is a
member of a cycle, the cycle number is printed
between the name and the index number.
If there are any cycles (circles) in the call graph, there is an
entry for the cycle-as-a-whole. This entry shows who called the
cycle (as parents) and the members of the cycle (as children.)
The `+' recursive calls entry shows the number of function calls that
were internal to the cycle, and the calls entry for each member shows,
for that member, how many times it was called from other members of
the cycle.
Copyright (C) 2012-2026 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
Index by function name
[11] _init [16] generer_transactions [3] nb_paires_opposees
[6] ajouter_ligne [7] longueur [5] rechercher_par_id
[14] alea [8] meilleure_periode [10] total_par_client
[13] categorie [9] nb_clients_distincts [12] total_par_jour
[15] empreinte [4] nb_facons_rendre [2] trier_par_montant
+6017
View File
File diff suppressed because it is too large Load Diff