Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Nadi Tomeh
SDA
Commits
385b4e7c
Commit
385b4e7c
authored
Nov 22, 2021
by
Rafik_Hammoutene
Browse files
AJout des fichier dynamic.c
parent
35eb00af
Pipeline
#4660
failed with stage
in 6 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
C/dynamic_tas_extract.c
0 → 100644
View file @
385b4e7c
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "fixed_tas_extract.h"
// Creation du tas binaire
tas_binaire_t
*
creer_tas_binaire
(){
tas_binaire_t
*
tas
=
(
tas_binaire_t
*
)
malloc
(
sizeof
(
tas_binaire_t
)
);
if
(
!
tas
)
return
NULL
;
tas
->
tab
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
4
);
tas
->
taille
=
4
;
tas
->
size
=
0
;
tas
->
echange
=
0
;
return
tas
;
}
// Le pere d'une clé dans un tas binaire
int
tas_binaire_pere
(
int
clef
)
{
return
(
int
)
floor
(
clef
/
2
);
}
// Destroy
void
tas_binaire_detruire
(
tas_binaire_t
*
t
){
if
(
t
!=
NULL
){
if
(
t
->
tab
!=
NULL
){
free
(
t
->
tab
);
}
free
(
t
);
}
}
int
tas_diminuer_clef
(
tas_binaire_t
*
t
,
int
cle
,
int
val
){
t
->
tab
[
cle
]
=
val
;
int
nombre_echange
=
0
;
while
(
cle
>
0
&&
t
->
tab
[
tas_binaire_pere
(
cle
)]
>
t
->
tab
[
cle
]){
int
tmp
=
t
->
tab
[
tas_binaire_pere
(
cle
)];
t
->
tab
[
tas_binaire_pere
(
cle
)]
=
t
->
tab
[
cle
];
t
->
tab
[
cle
]
=
tmp
;
nombre_echange
++
;
cle
=
tas_binaire_pere
(
cle
);
}
return
nombre_echange
;
}
char
tas_append
(
tas_binaire_t
*
t
,
int
x
){
char
memory_allocation
=
FALSE
;
if
(
t
!=
NULL
){
if
(
tas_do_we_need_to_enlarge_capacite
(
t
)){
memory_allocation
=
TRUE
;
tas_enlarge_capacite
(
t
);
}
t
->
echange
=
tas_diminuer_clef
(
t
,
t
->
size
,
x
);
t
->
size
=
t
->
size
+
1
;
}
return
memory_allocation
;
}
int
tasbinaire_get
(
tas_binaire_t
*
t
,
int
pos
){
if
(
t
!=
NULL
&&
pos
>
0
&&
pos
<
t
->
size
){
return
t
->
tab
[
pos
];
}
printf
(
"Wrong paramaeter pos=%d or null list"
,
pos
);
return
-
1
;
}
// pour mettre dans le tas
int
extraire_heap_minimum
(
tas_binaire_t
*
t
){
int
cle
=
0
;
//int cle_min;
//cle_min=t->tab[0];
int
nombre_echange
=
0
;
int
temp
=
0
;
int
fg
=
2
*
cle
+
1
;
int
fd
=
2
*
cle
+
2
;
while
((
cle
<
(
tas_binaire_size
(
t
)
-
1
))
&&
(
fg
<
tas_binaire_size
(
t
)
&&
fd
<
tas_binaire_size
(
t
))
&&
(
t
->
tab
[
cle
]
>
t
->
tab
[
fg
]
||
t
->
tab
[
cle
]
>
t
->
tab
[
fd
])){
if
(
t
->
tab
[
fg
]
>
t
->
tab
[
fd
]){
// si fg>fd alors
temp
=
t
->
tab
[
cle
];
t
->
tab
[
cle
]
=
t
->
tab
[
fd
];
t
->
tab
[
fd
]
=
temp
;
cle
=
fd
;
}
else
{
//Sinon
temp
=
t
->
tab
[
cle
];
t
->
tab
[
cle
]
=
t
->
tab
[
fg
];
t
->
tab
[
fg
]
=
temp
;
cle
=
fg
;
}
nombre_echange
++
;
//Remise en orde des fd et fg
fg
=
2
*
cle
+
1
;
fd
=
2
*
cle
+
2
;
}
return
nombre_echange
;
}
char
tas_pop_back
(
tas_binaire_t
*
t
){
char
memory_reduction
=
FALSE
;
if
(
t
!=
NULL
&&
t
->
size
>
0
){
if
(
tas_do_we_need_to_reduce_capacite
(
t
)){
memory_reduction
=
TRUE
;
tas_reduce_capacite
(
t
);
}
t
->
tab
[
0
]
=
t
->
tab
[
tas_binaire_size
(
t
)
-
1
];
t
->
size
--
;
t
->
echange
=
extraire_heap_minimum
(
t
);
}
return
memory_reduction
;
}
// Nombre d elem stockés dans le tas binaire
size_t
tas_binaire_size
(
tas_binaire_t
*
t
){
return
(
t
!=
NULL
)
?
t
->
size
:
-
1
;
}
// la capacité du tas binaire
size_t
tas_binaire_capacite
(
tas_binaire_t
*
t
){
return
(
t
!=
NULL
)
?
t
->
taille
:
-
1
;
}
char
tas_do_we_need_to_enlarge_capacite
(
tas_binaire_t
*
t
){
return
t
->
size
==
t
->
taille
?
TRUE
:
FALSE
;
}
void
tas_enlarge_capacite
(
tas_binaire_t
*
t
){
t
->
taille
=
t
->
taille
*
2
;
t
->
tab
=
(
int
*
)
realloc
(
t
->
tab
,
sizeof
(
int
)
*
t
->
taille
);
}
char
tas_do_we_need_to_reduce_capacite
(
tas_binaire_t
*
t
){
return
(
t
->
size
<=
t
->
taille
/
4
&&
t
->
size
>
4
)
?
TRUE
:
FALSE
;
}
void
tas_reduce_capacite
(
tas_binaire_t
*
t
){
t
->
taille
/=
2
;
t
->
tab
=
(
int
*
)
realloc
(
t
->
tab
,
sizeof
(
int
)
*
t
->
taille
);
}
C/dynamic_tas_extract_main.c
0 → 100644
View file @
385b4e7c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "arraylist.h"
#include "analyzer.h"
#include "dynamic_tas_extract.h"
int
main
(
int
argc
,
char
**
argv
){
int
i
;
// Creation du tas binaire
tas_binaire_t
*
a
=
creer_tas_binaire
();
// Analyse du temps pris par les opérations.
analyzer_t
*
time_analysis
=
analyzer_create
();
// Analyse du nombre de copies faites par les opérations.
analyzer_t
*
copy_analysis
=
analyzer_create
();
// Analyse de l'espace mémoire inutilisé.
analyzer_t
*
memory_analysis
=
analyzer_create
();
// Analyse du nombre d'echange effectuée.
analyzer_t
*
swap_analysis
=
analyzer_create
();
// Mesure de la durée d'une opération.
struct
timespec
before
,
after
;
clockid_t
clk_id
=
CLOCK_REALTIME
;
// utilisé comme booléen pour savoir si une allocation a été effectuée.
char
memory_allocation
;
srand
(
time
(
NULL
));
float
random_number
;
// var aléatoire
float
proba
=
0
.
9
;
// Probabilité d'insertion à 0.9
int
nombre_echange
;
// Nombre de swipe effectués
int
nombreDansTas
;
//Nombre qu'on insère dans le tas
for
(
i
=
0
;
i
<
900000
;
i
++
){
random_number
=
(
float
)
rand
()
/
(
float
)
RAND_MAX
;
nombreDansTas
=
rand
()
%
900000
;
if
(
random_number
<
proba
){
// Ajout d'un élément et mesure du temps pris par l'opération.
clock_gettime
(
clk_id
,
&
before
);
memory_allocation
=
tas_append
(
a
,
nombreDansTas
);
clock_gettime
(
clk_id
,
&
after
);
// Enregistrement du temps pris par l'opération
analyzer_append
(
time_analysis
,
after
.
tv_nsec
-
before
.
tv_nsec
);
// Enregistrement du nombre de copies efféctuées par l'opération.
// S'il y a eu réallocation de mémoire, il a fallu recopier tout le tableau.
analyzer_append
(
copy_analysis
,
(
memory_allocation
)
?
i
:
1
);
// Enregistrement de l'espace mémoire non-utilisé.
analyzer_append
(
memory_analysis
,
tas_binaire_capacite
(
a
)
-
tas_binaire_size
(
a
));
// Enregistrement du nombre d'echange effectuée
analyzer_append
(
swap_analysis
,
tas_binaire_echange
(
a
));
}
else
{
clock_gettime
(
clk_id
,
&
before
);
memory_allocation
=
tas_pop_back
(
a
);
clock_gettime
(
clk_id
,
&
after
);
// Enregistrement du temps pris par l'opération
analyzer_append
(
time_analysis
,
after
.
tv_nsec
-
before
.
tv_nsec
);
// Enregistrement du nombre de copies efféctuées par l'opération.
// S'il y a eu réallocation de mémoire, il a fallu recopier tout le tableau.
analyzer_append
(
copy_analysis
,
(
memory_allocation
)
?
i
:
1
);
// Enregistrement de l'espace mémoire non-utilisé.
analyzer_append
(
memory_analysis
,
tas_binaire_capacite
(
a
)
-
tas_binaire_size
(
a
));
// Enregistrement du nombre d'echange effectuée
analyzer_append
(
swap_analysis
,
tas_binaire_echange
(
a
));
}
}
// Affichage de quelques statistiques sur l'expérience.
fprintf
(
stderr
,
"Total cost: %Lf
\n
"
,
get_total_cost
(
time_analysis
));
fprintf
(
stderr
,
"Average cost: %Lf
\n
"
,
get_average_cost
(
time_analysis
));
fprintf
(
stderr
,
"Variance: %Lf
\n
"
,
get_variance
(
time_analysis
));
fprintf
(
stderr
,
"Standard deviation: %Lf
\n
"
,
get_standard_deviation
(
time_analysis
));
// Sauvegarde les données de l'expérience.
save_values
(
time_analysis
,
"../plots/dynamic_tas_time_p0.9.plot"
);
save_values
(
swap_analysis
,
"../plots/dynamic_tas_swap_p0.9.plot"
);
save_values
(
memory_analysis
,
"../plots/dynamic_tas_memory_p0.9.plot"
);
// Nettoyage de la mémoire avant la sortie du programme
tas_binaire_detruire
(
a
);
analyzer_destroy
(
time_analysis
);
analyzer_destroy
(
copy_analysis
);
analyzer_destroy
(
memory_analysis
);
analyzer_destroy
(
swap_analysis
);
return
EXIT_SUCCESS
;
}
C/fixed_tas_extract_main.c
View file @
385b4e7c
...
...
@@ -4,7 +4,7 @@
#include "arraylist.h"
#include "analyzer.h"
#include "
tas_binaire
.h"
#include "
fixed_tas_extract
.h"
int
main
(
int
argc
,
char
**
argv
){
...
...
C/main.c
View file @
385b4e7c
...
...
@@ -4,14 +4,14 @@
#include "arraylist.h"
#include "analyzer.h"
#include "
tas_binaire
.h"
#include "
dynamic_tas_extract
.h"
int
main
(
int
argc
,
char
**
argv
){
int
i
;
// Creation du tas binaire
tas_binaire_t
*
a
=
creer_tas_binaire
(
900000
);
tas_binaire_t
*
a
=
creer_tas_binaire
();
// Analyse du temps pris par les opérations.
analyzer_t
*
time_analysis
=
analyzer_create
();
// Analyse du nombre de copies faites par les opérations.
...
...
@@ -24,10 +24,10 @@ int main(int argc,char **argv){
struct
timespec
before
,
after
;
clockid_t
clk_id
=
CLOCK_REALTIME
;
// utilisé comme booléen pour savoir si une allocation a été effectuée.
//
char memory_allocation;
char
memory_allocation
;
srand
(
time
(
NULL
));
float
random_number
;
// var aléatoire
float
proba
=
0
.
8
;
// Probabilité d'insertion à 0.
8
float
proba
=
0
.
9
;
// Probabilité d'insertion à 0.
9
int
nombre_echange
;
// Nombre de swipe effectués
int
nombreDansTas
;
//Nombre qu'on insère dans le tas
...
...
@@ -40,13 +40,13 @@ int main(int argc,char **argv){
if
(
random_number
<
proba
){
// Ajout d'un élément et mesure du temps pris par l'opération.
clock_gettime
(
clk_id
,
&
before
);
nombre_echange
=
tas_append
(
a
,
nombreDansTas
);
memory_allocation
=
tas_append
(
a
,
nombreDansTas
);
clock_gettime
(
clk_id
,
&
after
);
// Enregistrement du temps pris par l'opération
analyzer_append
(
time_analysis
,
after
.
tv_nsec
-
before
.
tv_nsec
);
// Enregistrement du nombre de copies efféctuées par l'opération.
// S'il y a eu réallocation de mémoire, il a fallu recopier tout le tableau.
//
analyzer_append(copy_analysis, (memory_allocation)? i:1 );
analyzer_append
(
copy_analysis
,
(
memory_allocation
)
?
i
:
1
);
// Enregistrement de l'espace mémoire non-utilisé.
analyzer_append
(
memory_analysis
,
tas_binaire_capacite
(
a
)
-
tas_binaire_size
(
a
));
// Enregistrement du nombre d'echange effectuée
...
...
@@ -55,14 +55,14 @@ int main(int argc,char **argv){
}
else
{
clock_gettime
(
clk_id
,
&
before
);
nombre_echange
=
tas_pop_back
(
a
);
memory_allocation
=
tas_pop_back
(
a
);
clock_gettime
(
clk_id
,
&
after
);
// Enregistrement du temps pris par l'opération
analyzer_append
(
time_analysis
,
after
.
tv_nsec
-
before
.
tv_nsec
);
// Enregistrement du nombre de copies efféctuées par l'opération.
// S'il y a eu réallocation de mémoire, il a fallu recopier tout le tableau.
//
analyzer_append(copy_analysis, (memory_allocation)? i:1 );
analyzer_append
(
copy_analysis
,
(
memory_allocation
)
?
i
:
1
);
// Enregistrement de l'espace mémoire non-utilisé.
analyzer_append
(
memory_analysis
,
tas_binaire_capacite
(
a
)
-
tas_binaire_size
(
a
));
// Enregistrement du nombre d'echange effectuée
...
...
@@ -78,15 +78,15 @@ int main(int argc,char **argv){
fprintf
(
stderr
,
"Standard deviation: %Lf
\n
"
,
get_standard_deviation
(
time_analysis
));
// Sauvegarde les données de l'expérience.
save_values
(
time_analysis
,
"../plots/
fixed
_tas_time_p0.
8
.plot"
);
save_values
(
swap_analysis
,
"../plots/
fixed
_tas_swap_p0.
8
.plot"
);
save_values
(
memory_analysis
,
"../plots/
fixed
_tas_memory_p0.
8
.plot"
);
save_values
(
time_analysis
,
"../plots/
dynamic
_tas_time_p0.
9
.plot"
);
save_values
(
swap_analysis
,
"../plots/
dynamic
_tas_swap_p0.
9
.plot"
);
save_values
(
memory_analysis
,
"../plots/
dynamic
_tas_memory_p0.
9
.plot"
);
// Nettoyage de la mémoire avant la sortie du programme
tas_binaire_detruire
(
a
);
analyzer_destroy
(
time_analysis
);
//
analyzer_destroy(copy_analysis);
analyzer_destroy
(
copy_analysis
);
analyzer_destroy
(
memory_analysis
);
analyzer_destroy
(
swap_analysis
);
return
EXIT_SUCCESS
;
...
...
plots/plot_result
View file @
385b4e7c
...
...
@@ -12,9 +12,9 @@ set xlabel "Nombre d'elements ajoutes" font "Helvetica,24"
set ylabel "Temps amorti" font "Helvetica,24"
# Nom du fichier Postscript en sortie
set output '
fixed
_binary_heap_p0.
8
_extract_amortized_time_C.pdf'
set output '
dynamic
_binary_heap_p0.
9
_extract_amortized_time_C.pdf'
plot [0:100][0:100] '
fixed
_tas_time_p0.
8
.plot' using 1:3 w lines title "Amortized C avec p=0.
8
"
plot [0:100][0:100] '
dynamic
_tas_time_p0.
9
.plot' using 1:3 w lines title "Amortized C avec p=0.
9
"
###############################################################
########### Affichage de l'espace mémoire gaspillé ###########
...
...
@@ -23,9 +23,9 @@ plot [0:100][0:100] 'fixed_tas_time_p0.8.plot' using 1:3 w lines title "Amortize
set ylabel "Memoire gaspillee" font "Helvetica,24"
# Nom du fichier Postscript en sortie
set output '
fixed
_binary_heap_p0.
8
_extract_wasted_memory_C.pdf'
set output '
dynamic
_binary_heap_p0.
9
_extract_wasted_memory_C.pdf'
plot [0:300][0:300] '
fixed
_tas_memory_p0.
8
.plot' using 1:2 w lines title "Espace memoire inutilise C avec p=0.
8
"
plot [0:300][0:300] '
dynamic
_tas_memory_p0.
9
.plot' using 1:2 w lines title "Espace memoire inutilise C avec p=0.
9
"
#################################################################
########### Affichage du nombre de copies effectuées ###########
#################################################################
...
...
@@ -33,8 +33,8 @@ plot [0:300][0:300] 'fixed_tas_memory_p0.8.plot' using 1:2 w lines title "Espace
set ylabel "Nombre de copies effectuees" font "Helvetica,24"
# Nom du fichier Postscript en sortie
set output '
fixed
_binary_heap_p0.
8
_extract_swap_C.pdf'
set output '
dynamic
_binary_heap_p0.
9
_extract_swap_C.pdf'
plot '
fixed
_tas_swap_p0.
8
.plot' using 1:2 w boxes title "Nombre d'echange effectuees C avec p=0.
8
"
plot '
dynamic
_tas_swap_p0.
9
.plot' using 1:2 w boxes title "Nombre d'echange effectuees C avec p=0.
9
"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment