void div_con_dynamic(int i, int x, int y, int optL, int optR){ if(y < x) return; else if(y == x){ R[i][x] = computeR(i, x, optL, optR); } else { int m = (x+y)/2; R[i][m] = computeR(i,m,optL,optR); div_con_dynamic(i, x, m-1, optL, R[i][m]); div_con_dynamic(i, m+1,y,R[i][m], optR); } }
int main () { double m[1000*1000]; int i,j; double* p = m; for (i=0; i<1000; i++) { for (j=0; j<1000; j++) { *p = rand(); p++; } } clock_t t; double res=0; t = clock(); for (i=0; i<1000; i++) { res += computeR(m,1000); } printf ("%f\n", res); t = clock() - t; printf ("%f milliseconds\n",1000 * (((float)t)/CLOCKS_PER_SEC/1000)); }
int fast_dynamic_program(int n, int k){ compute_dissimilarity(n); int i = 0, j = 0, l = 0, tmp; for( i = 0 ; i < n ; i ++){ memset(C[i], 0, sizeof(C[i])); } for (j = 1; j < n; j++){ C[0][j] = D[0][j]; } for(i = 1; i < k; i++){ R[i][i] = i; R[i][n-1] = computeR(i,n-1, i, n-1); div_con_dynamic(i, i+1, n-2, i, R[i][n-1]); } return C[k-1][n-1]; }