Compare commits

2 Commits
main ... main

Author SHA1 Message Date
James Boutaric
1bf1d139d7 TD1.md 2025-09-10 16:32:27 +02:00
James Boutaric
e7c653710e TD1.md 2025-09-10 16:30:34 +02:00
9 changed files with 155 additions and 4 deletions

110
TD1.md Normal file
View File

@@ -0,0 +1,110 @@
# 📊 TD1 Analyse de performance et optimisation en C
## 🚀 Étapes initiales
Nous avons commencé par récupérer les programmes depuis le dépôt Git de **Maxime Menault**, puis nous les avons compilés et exécutés avec différentes valeurs.
Pour obtenir un temps dexécution **suffisamment long pour lanalyse**, nous avons choisi `1000` et `1000` comme valeurs de test.
```bash
gcc -g -pg -o student_rank student_rank.c heapsort.c bubblesort.c
./student_rank 5000 1000 0
```
Ensuite, nous avons utilisé **gprof** pour analyser le programme :
```bash
gprof ./student_rank
```
---
## 📈 Analyse avec gprof
### 🔹 Flat Profile
Le *Flat Profile* donne un aperçu global :
- Temps et nombre dexécutions par fonction
- Temps cumulé au fil des appels
- Nombre dappels effectués
- Temps moyen par appel (ms)
- Nom de la fonction
👉 Exemple (extrait) :
```
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
79.80 2.35 2.35 1001000 0.00 0.00 bubblesort
19.35 2.92 0.57 1000000 0.00 0.00 find_rank_student
```
### 🔹 Call Graph
Le *Call Graph* montre :
- Les relations entre les fonctions
- Le temps consommé par chaque fonction et ses enfants
- Le nombre total dappels
- Une vision hiérarchique (*arbre dexécution*)
👉 Exemple (extrait) :
```
[1] 100.0 0.00 2.96 main [1]
0.01 2.92 1/1 sort_students [2]
-----------------------------------------------
[2] 99.3 0.01 2.92 1 sort_students [2]
0.57 2.35 1000000/1000000 find_rank_student [3]
-----------------------------------------------
[3] 98.9 0.57 2.35 1000000 find_rank_student [3]
2.35 0.00 1000000/1001000 bubblesort [4]
```
---
## 🔎 Résultats de lanalyse
- La fonction **la plus lente est `bubblesort`** (≈80% du temps total).
- Elle est appelée **plus d1 million de fois** par `find_rank_student`.
- Le tri est donc **la source principale de ralentissement**.
---
## 🛠️ Optimisations
### ✅ Correction du bug dans `bubblesort`
Le code initial validait sa condition en permanence.
Solution : vérifier quil **ny a plus aucun échange** avant darrêter la boucle.
```c
void bubblesort(int* array, int length)
{
int swapped, i, tmp;
do {
swapped = 0;
for(i = 1; i < length; i++) {
if(array[i-1] > array[i]) {
tmp = array[i-1];
array[i-1] = array[i];
array[i] = tmp;
swapped++;
}
}
} while(swapped > 0);
}
```
### 🚀 Remplacement de `bubblesort` par `heapsort`
En remplaçant **bubblesort** par **heapsort**, le temps dexécution diminue drastiquement.
👉 Le tri devient **beaucoup plus rapide et plus efficace**.
---
## 📌 Résumé
- **Flat Profile** → temps et nb dexécutions par fonction (ici `bubblesort ≈ 80%`).
- **Call Graph** → arbre dappels entre fonctions + temps consommé.
- **Optimisation principale** :
- Corriger la condition de `bubblesort`.
- Remplacer `bubblesort` par `heapsort` pour de meilleures performances.
---
✍️ *Travail effectué dans le cadre du TD1 Analyse de performance et optimisation en C.*

View File

@@ -17,5 +17,5 @@ void bubblesort(int* array, int length)
swapped++;
}
}
} while(swapped==1);
} while(swapped> 0);
}

View File

@@ -43,7 +43,7 @@ void sift(int* array, int node, int length)
}
}
void heapsort(int* array, int length)
void heapsort2(int* array, int length)
{
int i = 0;
int temp_value = 0;

View File

@@ -1,7 +1,7 @@
#ifndef __HEAPSORT__
#define __HEAPSORT__
void heapsort(int* array, int length);
void heapsort2(int* array, int length);
void generate_array(int* array, int length);
void print_array(int* array, int length);

BIN
student_rank Executable file

Binary file not shown.

View File

@@ -55,7 +55,6 @@ int find_rank_student(int student_grade, int* grades_array, int students_number)
{
int position = -1;
int i = 0;
bubblesort(grades_array,students_number);
for(i = students_number-1; i >= 0; i--)
{
if(grades_array[i] == student_grade)

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.student_rank</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

View File

@@ -0,0 +1,22 @@
---
triple: 'arm64-apple-darwin'
binary-path: student_rank
relocations:
- { offsetInCU: 0x26, offset: 0x26, size: 0x8, addend: 0x0, symName: _generate_grades, symObjAddr: 0x0, symBinAddr: 0x10000348C, symSize: 0x88 }
- { offsetInCU: 0x52, offset: 0x52, size: 0x8, addend: 0x0, symName: _generate_grades, symObjAddr: 0x0, symBinAddr: 0x10000348C, symSize: 0x88 }
- { offsetInCU: 0xA0, offset: 0xA0, size: 0x8, addend: 0x0, symName: _generate_ranks, symObjAddr: 0x88, symBinAddr: 0x100003514, symSize: 0xC4 }
- { offsetInCU: 0xFC, offset: 0xFC, size: 0x8, addend: 0x0, symName: _free_array, symObjAddr: 0x14C, symBinAddr: 0x1000035D8, symSize: 0x68 }
- { offsetInCU: 0x13C, offset: 0x13C, size: 0x8, addend: 0x0, symName: _print_student_array, symObjAddr: 0x1B4, symBinAddr: 0x100003640, symSize: 0xA4 }
- { offsetInCU: 0x18A, offset: 0x18A, size: 0x8, addend: 0x0, symName: _find_rank_student, symObjAddr: 0x258, symBinAddr: 0x1000036E4, symSize: 0xAC }
- { offsetInCU: 0x1EA, offset: 0x1EA, size: 0x8, addend: 0x0, symName: _sort_students, symObjAddr: 0x304, symBinAddr: 0x100003790, symSize: 0x144 }
- { offsetInCU: 0x270, offset: 0x270, size: 0x8, addend: 0x0, symName: _main, symObjAddr: 0x448, symBinAddr: 0x1000038D4, symSize: 0x164 }
- { offsetInCU: 0x26, offset: 0x31C, size: 0x8, addend: 0x0, symName: _print_array, symObjAddr: 0x0, symBinAddr: 0x100003A38, symSize: 0xB4 }
- { offsetInCU: 0x33, offset: 0x329, size: 0x8, addend: 0x0, symName: _print_array, symObjAddr: 0x0, symBinAddr: 0x100003A38, symSize: 0xB4 }
- { offsetInCU: 0x54, offset: 0x34A, size: 0x8, addend: 0x0, symName: _print_array.count, symObjAddr: 0xC3C, symBinAddr: 0x100008004, symSize: 0x0 }
- { offsetInCU: 0x9D, offset: 0x393, size: 0x8, addend: 0x0, symName: _generate_array, symObjAddr: 0x2F0, symBinAddr: 0x100003D28, symSize: 0xAC }
- { offsetInCU: 0xBE, offset: 0x3B4, size: 0x8, addend: 0x0, symName: _generate_array.first_call, symObjAddr: 0x3B4, symBinAddr: 0x100008000, symSize: 0x0 }
- { offsetInCU: 0xF2, offset: 0x3E8, size: 0x8, addend: 0x0, symName: _sift, symObjAddr: 0xB4, symBinAddr: 0x100003AEC, symSize: 0x150 }
- { offsetInCU: 0x16A, offset: 0x460, size: 0x8, addend: 0x0, symName: _heapsort2, symObjAddr: 0x204, symBinAddr: 0x100003C3C, symSize: 0xEC }
- { offsetInCU: 0x26, offset: 0x4D9, size: 0x8, addend: 0x0, symName: _bubblesort, symObjAddr: 0x0, symBinAddr: 0x100003DD4, symSize: 0xE8 }
- { offsetInCU: 0x33, offset: 0x4E6, size: 0x8, addend: 0x0, symName: _bubblesort, symObjAddr: 0x0, symBinAddr: 0x100003DD4, symSize: 0xE8 }
...