Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d2f5c7106d | |||
| 533d7685ca |
@@ -0,0 +1,18 @@
|
|||||||
|
GPROF : FONCTIONNEMENT
|
||||||
|
|
||||||
|
Deux métriques sont utilisés lors des mesures : le temps, et le nombre d'appels.
|
||||||
|
Dans un premier tableau, dit Flat Profile, il donne la consommation du temps en pourcentage par rapport au temps d'exécution total, ainsi que le nombre d'appels sur ce même temps. Il fait cela en mesurant tous les centièmes de secondes.
|
||||||
|
|
||||||
|
D'après ce premier tableau, on constate que le tri bulle consomme nettement plus
|
||||||
|
que n'importe quelle autre fonction du programme, avec une consommation d'à peu près 80% du temps total d'exécution. La deuxième fonction consommant le plus est find_rank_student, avec une consommation de 20%.
|
||||||
|
|
||||||
|
On a aussi le callgraph, qui permet de hiérarchiser les fonctions selon qui appelle qui, en indiquant le nombre d'appels et les temps d'exécution individuels de chaque sous-fonctions.
|
||||||
|
|
||||||
|
Ici, on voit que la fonction bubblesort est appelé 1 million de fois par find_rank_student; c'est sûrement ce qui pose un problème.
|
||||||
|
|
||||||
|
Cela nous permet de voir qu'il y a un appel en trop de bubblesort dans find_rank_student. Une fois retiré, et Gprof relancé, bubblesort n'est appelé plus que mille fois. Mais, l'algorithme reste toujours trop lourd, et continue de consomer près de 80% du temps d'exécution total. En replaçant l'algorithme par un plus optimisé, comme le heapsort, les performances sont largement meilleures. Il n'y a plus qu'une dizaines d'opérations effectuées, et le temps de consommation est quasiment nulle pour toutes les fonctions.
|
||||||
|
|
||||||
|
L'ensemble de ces résultats sont valables pour 1000 étudiants et 1000 notes chacuns. Pour 10000 et 10000, même avec un tri par tas, l'exécution consomme bien plus.
|
||||||
|
|
||||||
|
|
||||||
|
On va donc devoir changer l'algorithmie.
|
||||||
+1
-1
@@ -14,7 +14,7 @@ void bubblesort(int* array, int length)
|
|||||||
tmp = array[i-1];
|
tmp = array[i-1];
|
||||||
array[i-1] = array[i];
|
array[i-1] = array[i];
|
||||||
array[i] = tmp;
|
array[i] = tmp;
|
||||||
swapped++;
|
swapped=1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while(swapped==1);
|
} while(swapped==1);
|
||||||
|
|||||||
+67
@@ -0,0 +1,67 @@
|
|||||||
|
/*algorithm quicksort(A, lo, hi) is
|
||||||
|
// Ensure indices are in correct order
|
||||||
|
if lo >= hi || lo < 0 then
|
||||||
|
return
|
||||||
|
|
||||||
|
// Partition array and get the pivot index
|
||||||
|
p := partition(A, lo, hi)
|
||||||
|
|
||||||
|
// Sort the two partitions
|
||||||
|
quicksort(A, lo, p - 1) // Left side of pivot
|
||||||
|
quicksort(A, p + 1, hi) // Right side of pivot
|
||||||
|
|
||||||
|
// Divides array into two partitions
|
||||||
|
algorithm partition(A, lo, hi) is
|
||||||
|
pivot := A[hi] // Choose the last element as the pivot
|
||||||
|
|
||||||
|
// Temporary pivot index
|
||||||
|
i := lo
|
||||||
|
|
||||||
|
for j := lo to hi - 1 do
|
||||||
|
// If the current element is less than or equal to the pivot
|
||||||
|
if A[j] <= pivot then
|
||||||
|
// Swap the current element with the element at the temporary pivot index
|
||||||
|
swap A[i] with A[j]
|
||||||
|
// Move the temporary pivot index forward
|
||||||
|
i := i + 1
|
||||||
|
|
||||||
|
// Swap the pivot with the last element
|
||||||
|
swap A[i] with A[hi]
|
||||||
|
return i // the pivot index*/
|
||||||
|
|
||||||
|
int Partition(int * array, int lo, int hi){
|
||||||
|
/*variables*/
|
||||||
|
int pivot = array[hi];
|
||||||
|
int tmp_ind = lo;
|
||||||
|
int tmp;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
/*pivots*/
|
||||||
|
for(i=lo; i<hi; i++){
|
||||||
|
if(array[i]<=pivot){
|
||||||
|
tmp = array[i];
|
||||||
|
array[i] = array[tmp_ind];
|
||||||
|
array[tmp_ind] = tmp;
|
||||||
|
tmp_ind++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmp = array[tmp_ind];
|
||||||
|
array[tmp_ind] = array[hi];
|
||||||
|
array[hi] = tmp;
|
||||||
|
return tmp_ind;
|
||||||
|
}
|
||||||
|
|
||||||
|
void quicksort(int * array, int lo, int hi){
|
||||||
|
/*variables*/
|
||||||
|
int p;
|
||||||
|
|
||||||
|
/*test - indices dans l'ordre*/
|
||||||
|
if((lo>=hi)||(lo<0)){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
p = Partition(array, lo, hi);
|
||||||
|
|
||||||
|
quicksort(array, lo, p-1);
|
||||||
|
quicksort(array, p+1, hi);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef __QUICKSORT__
|
||||||
|
#define __QUICKSORT__
|
||||||
|
|
||||||
|
int Partition(int * array, int lo, int hi);
|
||||||
|
void quicksort(int * array, int lo, int hi);
|
||||||
|
|
||||||
|
#endif
|
||||||
Executable
BIN
Binary file not shown.
+4
-2
@@ -5,6 +5,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "heapsort.h"
|
#include "heapsort.h"
|
||||||
#include "bubblesort.h"
|
#include "bubblesort.h"
|
||||||
|
#include "quicksort.h"
|
||||||
|
|
||||||
void generate_grades(int** students_array, int students_number, int grades_number)
|
void generate_grades(int** students_array, int students_number, int grades_number)
|
||||||
{
|
{
|
||||||
@@ -55,7 +56,7 @@ int find_rank_student(int student_grade, int* grades_array, int students_number)
|
|||||||
{
|
{
|
||||||
int position = -1;
|
int position = -1;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
bubblesort(grades_array,students_number);
|
(grades_array,students_number);
|
||||||
for(i = students_number-1; i >= 0; i--)
|
for(i = students_number-1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
if(grades_array[i] == student_grade)
|
if(grades_array[i] == student_grade)
|
||||||
@@ -77,7 +78,7 @@ void sort_students(int** students_rank, int** students_array, int students_numbe
|
|||||||
{
|
{
|
||||||
grades[j] = students_array[j][i];
|
grades[j] = students_array[j][i];
|
||||||
}
|
}
|
||||||
bubblesort(grades,students_number);
|
quicksort(grades,0,students_number);
|
||||||
for(j = 0; j < students_number; j++)
|
for(j = 0; j < students_number; j++)
|
||||||
{
|
{
|
||||||
students_rank[j][i] = find_rank_student(students_array[j][i],grades,students_number);
|
students_rank[j][i] = find_rank_student(students_array[j][i],grades,students_number);
|
||||||
@@ -93,6 +94,7 @@ int main(int argc, char** argv)
|
|||||||
int students_length = 0;
|
int students_length = 0;
|
||||||
int grades_length = 0;
|
int grades_length = 0;
|
||||||
int debug_mode = 0;
|
int debug_mode = 0;
|
||||||
|
int* ranker = NULL;
|
||||||
|
|
||||||
if(argc != 4)
|
if(argc != 4)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user