From cd34f395351871af01c81497a7a34a647eca412c Mon Sep 17 00:00:00 2001 From: Ryad MEHIRI Date: Wed, 14 Oct 2026 14:00:02 +0200 Subject: [PATCH] =?UTF-8?q?Construire=20le=20rapport=20en=20temps=20lin?= =?UTF-8?q?=C3=A9aire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ajouter() recalculait la longueur du rapport à chaque ligne : 5 867 parcours d'une chaîne qui grandit jusqu'à ~90 Ko (O(n²) en caractères). On garde un pointeur sur la fin du rapport et on écrit directement à la suite avec sprintf. gprof n'attribuait que 0,04 s à construire_rapport, car il répartit le temps de longueur() au prorata du nombre d'appels : la mesure montre que c'était en réalité le premier poste restant. Temps : 2,0 s → 1,06 s. Empreinte inchangée (1d53de903efd2b7c). Co-Authored-By: Claude Opus 5.5 --- analyseur.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/analyseur.c b/analyseur.c index 0118d97..2205ba9 100644 --- a/analyseur.c +++ b/analyseur.c @@ -380,15 +380,11 @@ void analyser_bigrammes(char **lignes, int nb_lignes, char *construire_rapport(const Entree *table, int n) { char *rapport = malloc((size_t)n * 64 + 256); - char ligne[128]; - rapport[0] = '\0'; + char *fin = rapport; /* position du '\0' final : on écrit directement à la suite */ - ajouter(rapport, "rang;mot;occurrences\n"); - for (int i = 0; i < n; i++) { - snprintf(ligne, sizeof(ligne), "%d;%s;%d\n", - i + 1, table[i].mot, table[i].occurrences); - ajouter(rapport, ligne); - } + fin += sprintf(fin, "rang;mot;occurrences\n"); + for (int i = 0; i < n; i++) + fin += sprintf(fin, "%d;%s;%d\n", i + 1, table[i].mot, table[i].occurrences); return rapport; }