From 2eb0f7f1d7f61fbcbacf67223a59d59e2eacc847 Mon Sep 17 00:00:00 2001 From: Tom Moguljak Date: Tue, 3 Sep 2024 12:18:55 +0200 Subject: [PATCH] Td algo --- gmon.out | Bin 0 -> 3789 bytes note.txt | 286 +++++++++++++++++++++++++++++++++++++++++++++++++ student_rank | Bin 0 -> 22760 bytes student_rank.c | 2 +- 4 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 gmon.out create mode 100644 note.txt create mode 100644 student_rank diff --git a/gmon.out b/gmon.out new file mode 100644 index 0000000000000000000000000000000000000000..0bd6f3698be5e79a2654257bea1b9d36c2c01136 GIT binary patch literal 3789 zcmYe#&Cg?GzyVH3LnOYiF)*Y6nZ>Ee`FSbDFx7AdOkk8A4S~@R7!85Z5Eu=C(GVC7 zfzc2cOd-HMC>(>cvB|)~0Fq;5U}oS2(yU-M69XGqlsZo5Akr9Ma~CII$0;)^F&YBI zhXC$;fLjLDbS6|ToJWj)V${G5WMDiX1mUPiLFgCEz>HcT3gH}s3JRzL1s^~KOGO}( v4bx^YFn$n&a3Y|_fNW +[2] 100.0 0.00 6.15 main [2] + 0.04 6.11 1/1 sort_students [1] + 0.00 0.00 2/2 free_array [6] + 0.00 0.00 1/1 generate_grades [7] + 0.00 0.00 1/1 generate_ranks [8] +----------------------------------------------- + 1.00 5.10 1000000/1000000 sort_students [1] +[3] 99.3 1.00 5.10 1000000 find_rank_student [3] + 5.10 0.00 1000000/1001000 bubblesort [4] +----------------------------------------------- + 0.01 0.00 1000/1001000 sort_students [1] + 5.10 0.00 1000000/1001000 find_rank_student [3] +[4] 83.1 5.11 0.00 1001000 bubblesort [4] +----------------------------------------------- + 0.00 0.00 1000/1000 generate_grades [7] +[5] 0.0 0.00 0.00 1000 generate_array [5] +----------------------------------------------- + 0.00 0.00 2/2 main [2] +[6] 0.0 0.00 0.00 2 free_array [6] +----------------------------------------------- + 0.00 0.00 1/1 main [2] +[7] 0.0 0.00 0.00 1 generate_grades [7] + 0.00 0.00 1000/1000 generate_array [5] +----------------------------------------------- + 0.00 0.00 1/1 main [2] +[8] 0.0 0.00 0.00 1 generate_ranks [8] +----------------------------------------------- + +On peut voir que la fonction bubblesort a été appelée 1001000 fois, 1000 fois par la fonction generate_array et 1000000 fois par la fonction find_rank_student. + +Par la suite on a essayé de lancer le programme une nouvelle fois avec des paramètres encore plus grands : +./student_rank 1000 10000 0 + +Ce qu'on obtient avec gprof : +Flat profile: + +Each sample counts as 0.01 seconds. + % cumulative self self total + time seconds seconds calls s/call s/call name + 83.84 52.32 52.32 10010000 0.00 0.00 bubblesort + 13.46 60.72 8.40 10000000 0.00 0.00 find_rank_student + 2.60 62.34 1.62 1 1.62 62.34 sort_students + 0.11 62.41 0.07 1 0.07 0.07 generate_ranks + 0.08 62.46 0.05 1000 0.00 0.00 generate_array + 0.02 62.47 0.01 print_student_array + 0.00 62.47 0.00 2 0.00 0.00 free_array + 0.00 62.47 0.00 1 0.00 0.05 generate_grades + + + Call graph + + +granularity: each sample hit covers 2 byte(s) for 0.02% of 62.47 seconds + +index % time self children called name + +[1] 100.0 0.00 62.46 main [1] + 1.62 60.72 1/1 sort_students [2] + 0.07 0.00 1/1 generate_ranks [5] + 0.00 0.05 1/1 generate_grades [7] + 0.00 0.00 2/2 free_array [9] +----------------------------------------------- + 1.62 60.72 1/1 main [1] +[2] 99.8 1.62 60.72 1 sort_students [2] + 8.40 52.27 10000000/10000000 find_rank_student [3] + 0.05 0.00 10000/10010000 bubblesort [4] +----------------------------------------------- + 8.40 52.27 10000000/10000000 sort_students [2] +[3] 97.1 8.40 52.27 10000000 find_rank_student [3] + 52.27 0.00 10000000/10010000 bubblesort [4] +----------------------------------------------- + 0.05 0.00 10000/10010000 sort_students [2] + 52.27 0.00 10000000/10010000 find_rank_student [3] +[4] 83.8 52.32 0.00 10010000 bubblesort [4] +----------------------------------------------- + 0.07 0.00 1/1 main [1] +[5] 0.1 0.07 0.00 1 generate_ranks [5] +----------------------------------------------- + 0.05 0.00 1000/1000 generate_grades [7] +[6] 0.1 0.05 0.00 1000 generate_array [6] +----------------------------------------------- + 0.00 0.05 1/1 main [1] +[7] 0.1 0.00 0.05 1 generate_grades [7] + 0.05 0.00 1000/1000 generate_array [6] +----------------------------------------------- + +[8] 0.0 0.01 0.00 print_student_array [8] +----------------------------------------------- + 0.00 0.00 2/2 main [1] +[9] 0.0 0.00 0.00 2 free_array [9] +----------------------------------------------- + + +Idées pour améliorer le programme : +- Appeler bubblesort moins souvent +- Optimiser bubblesort +- Appeler sort_students moins souvent car elle appelle bubblesort et find_rank_student qui sont les fonctions qui prennent le plus de temps + +On remarque aussi qu'on appelle bubblesort une fois en trop, car on l'appelle dans sort_students et dans find_rank_student. +On pourrait enlever l'appel de bubblesort dans find_rank_student afin qu'il ne soit appelé qu'une seule fois dans sort_students. + +On recompile et on relance le programme avec les paramètres suivants : +./student_rank 1000 10000 0 + +On relance gprof et on obtient le résultat suivant : +Flat profile: + +Each sample counts as 0.01 seconds. + % cumulative self self total + time seconds seconds calls ms/call ms/call name + 48.74 0.37 0.37 10000000 0.00 0.00 find_rank_student + 27.66 0.58 0.21 1 210.24 620.70 sort_students + 14.49 0.69 0.11 1 110.12 110.12 generate_ranks + 5.27 0.73 0.04 10000 0.00 0.00 bubblesort + 2.63 0.75 0.02 1000 0.02 0.02 generate_array + 1.32 0.76 0.01 print_student_array + 0.00 0.76 0.00 2 0.00 0.00 free_array + 0.00 0.76 0.00 1 0.00 20.02 generate_grades + + + Call graph + + +granularity: each sample hit covers 2 byte(s) for 1.31% of 0.76 seconds + +index % time self children called name + +[1] 98.7 0.00 0.75 main [1] + 0.21 0.41 1/1 sort_students [2] + 0.11 0.00 1/1 generate_ranks [4] + 0.00 0.02 1/1 generate_grades [7] + 0.00 0.00 2/2 free_array [9] +----------------------------------------------- + 0.21 0.41 1/1 main [1] +[2] 81.6 0.21 0.41 1 sort_students [2] + 0.37 0.00 10000000/10000000 find_rank_student [3] + 0.04 0.00 10000/10000 bubblesort [5] +----------------------------------------------- + 0.37 0.00 10000000/10000000 sort_students [2] +[3] 48.7 0.37 0.00 10000000 find_rank_student [3] +----------------------------------------------- + 0.11 0.00 1/1 main [1] +[4] 14.5 0.11 0.00 1 generate_ranks [4] +----------------------------------------------- + 0.04 0.00 10000/10000 sort_students [2] +[5] 5.3 0.04 0.00 10000 bubblesort [5] +----------------------------------------------- + 0.02 0.00 1000/1000 generate_grades [7] +[6] 2.6 0.02 0.00 1000 generate_array [6] +----------------------------------------------- + 0.00 0.02 1/1 main [1] +[7] 2.6 0.00 0.02 1 generate_grades [7] + 0.02 0.00 1000/1000 generate_array [6] +----------------------------------------------- + +[8] 1.3 0.01 0.00 print_student_array [8] +----------------------------------------------- + 0.00 0.00 2/2 main [1] +[9] 0.0 0.00 0.00 2 free_array [9] +----------------------------------------------- + +On peut voir qu'on a réussi à réduire le temps d'exécution de 62.47 secondes à 0.76 secondes. +On voit aussi que la fonction bubblesort est seulement appelée par la fonction sort_students et non plus par find_rank_student. + +Maintenant on va essayer d'optimiser la fonction bubblesort mais la fonction est déjà assez optimisée. \ No newline at end of file diff --git a/student_rank b/student_rank new file mode 100644 index 0000000000000000000000000000000000000000..a3d2d352adfeff0209ad78a89165f4d6114e5098 GIT binary patch literal 22760 zcmeHPdw3hwmA@l-Wcd+WiQ~k1P%9*XBw*Pl4+420Cs9y{0|9K-p@1UmVXN4(R~m(c z4G`icMW|_$wiLdWZL^fGbi0&~KE5`k>|*nZp_CdHXxio5HEh_GODO3!r7lg;{?2{K zqlvXk_n-a8*PrIxbMATFbI-kVXFM}^`C2#IG>xfZXX_cqrVEN!Hhk+E#le`Dt!C43 zT+9|R2goJ(d1bQ|p0J4q1)Y-54oGr!RG9?M7i6ZO@{l0O6)U6CfP%~=I7v>Ys>H4E zxeCFjplZ+CbBlInfsaf76*S9HY=#?z-0-?W_^6D_F$t9%p(J-g%AJsM3XYOPC<>~0 zQcUQ#TKZ#!ZnRNx^CM$cxeh63g_|V5f@Fir_3s$r|BJotQtkmaSH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1;3JN=`usgw66hy@GG2G(5A2ydqaZm~<)s z)%BGj<#B~jP^{xWUD~(yQ_0sCNDE(yL_SlQE9C_?ar~lGczA-uYf4U$mlly<*ep2e z)WAsrt2{kXV5Jf3n$<$!LreZzc{di3U%5ue|H%?Ct2{l7MKo*tn!G~(mLm3*o*RqE ze{O@2r+2N)MWJSBF~QjfHwvl4a$#Nwbh7oE)+d!aLP8w3UnSsgERD*p;-$-c3kz>r Pf3@JC`%LCyLD_!;khWu~ literal 0 HcmV?d00001 diff --git a/student_rank.c b/student_rank.c index af84003..85e04a7 100644 --- a/student_rank.c +++ b/student_rank.c @@ -55,7 +55,7 @@ int find_rank_student(int student_grade, int* grades_array, int students_number) { int position = -1; int i = 0; - bubblesort(grades_array,students_number); + //bubblesort(grades_array,students_number); for(i = students_number-1; i >= 0; i--) { if(grades_array[i] == student_grade)