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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include <Windows.h>
#include <cstdlib>
#include <cstdio>
#include <cmath>
// timer pour afficher les perfs
struct timer
{
LARGE_INTEGER _start, _stop, _freq ;
timer () { ::QueryPerformanceFrequency(&_freq); }
void start () { ::QueryPerformanceCounter(&_start); }
double stop ()
{
::QueryPerformanceCounter(&_stop);
return double (_stop.QuadPart - _start.QuadPart) / double (_freq.QuadPart) ;
}
};
// petite fonction d'allocation pour les double**
double** allocate(int n) ;
void desallocate(double** a, int size) ;
void fonction1 (); // la tienne (sans rien changer)
void fonction2(); // la mienne
// -------------- MAIN ------------------------
int main (int argc, char** argv)
{
fonction1 ();
fonction2 ();
}
// -------------- MAIN ------------------------
double** allocate(int n)
{
double** res = (double**) malloc(600*sizeof(double*));
for (int i=0; i<600; ++i)
res[i] = (double*) malloc(600*sizeof(double));
return res ;
}
void desallocate(double** a, int size)
{
for (int i = 0 ; i < size ; ++i)
free(a[i]);
free(a);
}
void fonction1 ()
{
timer tim ;
double** tabInitial = allocate(600);
double** tabFinal = allocate(600);
double** filter = allocate(21);
tim.start () ;
int demiTaille = 10;
for(int n=0; n< 600; ++n)
{
for(int m=0; m<600; ++m)
{
int uBegin = max(0, n - demiTaille );
int uEnd = min(599, n + demiTaille );
int vBegin = max(0, m - demiTaille );
int vEnd = min(599, m + demiTaille );
double temp = 0.0;
for(int u=uBegin; u<=uEnd; ++u) {
for(int v=vBegin; v<=vEnd; ++v) {
temp += abs(tabInitial[u][v]) * filter[demiTaille + u - n][demiTaille + v - m];
}
}
tabFinal[n][m] = temp;
}
}
::printf("fonction initiale : %f\n", tim.stop ());
desallocate(tabInitial, 600);
desallocate(tabFinal, 600);
desallocate(filter, 600);
}
void fonction2 ()
{
timer tim ;
int demiTaille = 10;
double* tabInitial = (double*) malloc(600*600*sizeof(double)) ;
double* tabFinal = (double*) malloc(600*600*sizeof(double)) ;
double* filter = (double*) malloc(21*21*sizeof(double));
tim.start () ;
int ind = 0 ;
for (int n = 0; n < 600; ++n) // loop on main matrix
{
for (int m = 0 ; m < 600 ; ++m, ++ind)
{
double temp = 0. ;
int uBegin = max(0, n - demiTaille );
int uEnd = min(599, n + demiTaille );
int vBegin = max(0, m - demiTaille );
int vEnd = min(599, m + demiTaille );
for(int u=uBegin; u<=uEnd; ++u) {
int uu=demiTaille+u -n ;
for(int v=vBegin; v<=vEnd; ++v) {
int vv=demiTaille+v-m;
temp += abs(tabInitial[u*600+v]) * filter[uu*21 + vv];
}
}
tabFinal[ind] = temp;
}
}
::printf("fonction mieux : %f\n", tim.stop ());
free(tabInitial);
free(tabFinal);
free(filter);
} |