Bonjour, je me suis fait une structure Matrix comme suit
Je me donne 3 matrices 1000*1000 (mat1, mat2 et mat3) remplies uniquement de 1. Lorsque j'effectue l'opération mat1*mat2 le temps d'exécution fait 0.62 s mais lorsque je fais mat3=mat1*mat2 le temps d'exécution passe à 16.79 s. Ca me paraît très bizarre... Savez-vous pourquoi ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 typedef struct t_mat { unsigned int nl; // nombre de lignes unsigned int nc; // nombre de colonnes double ** data; } Matrix; Matrix * CreateMatrix(unsigned int nl_,unsigned int nc_,double val) { /* memory allocation */ Matrix * m=malloc(sizeof(*m)); if(m==NULL) { MEMERROR; exit(EXIT_FAILURE); } /* an empty matrix is defined by nl==0, nc==0 and data==NULL */ if(nl_==0) if(nc_!=0) { fprintf (stderr,"%s %d : Error : nc_ must be equal to 0\n",__FILE__,__LINE__); exit(EXIT_FAILURE); } if(nc_==0) if(nl_!=0) { fprintf (stderr,"%s %d : Error : nl_ must be equal to 0\n",__FILE__,__LINE__); exit(EXIT_FAILURE); } if(nl_==0) { m->nl=0; m->nc=0; m->data=NULL; } else { m->nl=nl_; m->nc=nc_; m->data=malloc(nl_*sizeof(realtype *)); if(m->data==NULL) { MEMERROR; exit(EXIT_FAILURE); } unsigned int i,j; for(i=0;i<nl_;i++) { m->data[i]=malloc(nc_*sizeof(realtype)); if(m->data[i]==NULL) { MEMERROR; exit(EXIT_FAILURE); } for(j=0;j<nc_;j++) /* matrix initialisation */ m->data[i][j]=val; } } return m; } void DestroyMatrix(Matrix ** m) { (*m)->nc=0; (*m)->nl=0; /* free memory */ unsigned int i; for(i=0;i<(*m)->nc;i++) { if((*m)->data[i]!=NULL) { free((*m)->data[i]); (*m)->data[i]=NULL; } } free((*m)->data); (*m)->data=NULL; free(*m); *m=NULL; }
Voici le code pour faire mat1*mat2
et voici le code pour faire mat3=mat1*mat2
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 int main(int argc,char * argv[]) { Matrix * mat1=CreateMatrix(1000,1000,1); Matrix * mat2=CreateMatrix(1000,1000,1); double somme; unsigned i,jj; for(i=0;i<1000;++i) { for(jj=0;jj<1000;jj++) { somme=0.; for(k=0;k<1000;++k) somme+=mat1->data[i][k]*mat2->data[k][jj]; } } DestroyMatrix(&mat1); DestroyMatrix(&mat2); return 0; }
Merci.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 int main(int argc,char * argv[]) { Matrix * mat1=CreateMatrix(1000,1000,1); Matrix * mat2=CreateMatrix(1000,1000,1); Matrix * mat3=CreateMatrix(1000,1000,1); double somme; unsigned i,jj; for(i=0;i<1000;++i) { for(jj=0;jj<1000;jj++) { somme=0.; for(k=0;k<1000;++k) somme+=mat1->data[i][k]*mat2->data[k][jj]; mat3->data[i][jj]=somme; } } DestroyMatrix(&mat1); DestroyMatrix(&mat2); DestroyMatrix(&mat3); return 0; }
Partager