Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1556 connectés 

  FORUM HardWare.fr
  Programmation
  Algo

  Repartition decpu load "decoupe de rectangle", et C++

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Repartition decpu load "decoupe de rectangle", et C++

n°1170277
xiluoc
un pc pour les unirs ....
Posté le 05-08-2005 à 02:01:12  profilanswer
 

:hello: ,
j explique :
On donne un nombre P de processeurs
il y a NxM taches a partager.  
Le bute est de minimiser le total des tache que n importe quel pc recevra.  
Les contraintes sont :
- Chaque tache ne peus pas etre partage entre les pc.
- Toutes taches alouer a un pc doivent etre un sous rectangle de la matrix
 
 
http://img291.imageshack.us/img291/8534/exemple6km.png
 
Dans l example 1  
le max load est 12 ( 1 + 2 +3 + 1 + 2 +3 ) mais on vois bien que si on avait couper differement
1 2   et   3
1 2        3
1 2        3
 
on aurait eu 9 taches pour les 2 pc c etait parfait.
 
Ma question est vers quel algo regarder, je devrais peut etre choisir entre efficacite et reponses optimales.
Comment aborder le problem ? est ce un problem connu en algo ?
 
merci   :jap:


Message édité par xiluoc le 10-08-2005 à 00:24:08
mood
Publicité
Posté le 05-08-2005 à 02:01:12  profilanswer
 

n°1171223
xiluoc
un pc pour les unirs ....
Posté le 06-08-2005 à 03:49:42  profilanswer
 

up

n°1171242
xiluoc
un pc pour les unirs ....
Posté le 06-08-2005 à 10:01:08  profilanswer
 

apperement cest un problem np, la solution parfaite prendrais un peu trop de temps. jai trouve pas mal d algo de partitionement pour les graphs mais la cest une matrice simple.

n°1171502
xiluoc
un pc pour les unirs ....
Posté le 07-08-2005 à 00:55:56  profilanswer
 

:/

n°1172059
xiluoc
un pc pour les unirs ....
Posté le 08-08-2005 à 00:48:40  profilanswer
 

a vot bon coeur

n°1172071
phosphorus​68
Pseudo à n°
Posté le 08-08-2005 à 01:38:04  profilanswer
 

J'aurais fait des rectangles de 1x1 [:dawa]
Ensuite le problème se ramène à sommer toutes les valeurs de la matrice, diviser par le nombre de procs et à tomber le plus près possible juste un peu au-delà de (total/p)
 
Si j'ai bien lu ton énoncé, y aucune contrainte pour répondre comme cela [:joce] (ou ta phrase "toutes taches ..." ne respecte aucune règle syntaxique en vigueur dans notre beau pays :o)

n°1172078
dividee
Posté le 08-08-2005 à 03:18:18  profilanswer
 

Je connais pas grand chose à ce genre de problème, mais pourquoi pas appliquer "diviser pour régner" ? C'est peut-être simpliste, mais ça donne quand-même la solution optimale pour tous les exemples que tu as donné. Je l'ai testé sur des plus grosses matrices générées aléatoirement, et les résultats semblent pas trop mauvais...
 
En détail:
Si il y a qu'un seul processeur, c'est trivial
 
S'il y a deux processeurs, c'est facile: pour un matrice N x M, il n'y a que (N-1)+(M-1) partitionnements possibles, on peut les essayer tous. La meilleur répartition sera celle dont le rapport des charges sera la plus proche de 1.
 
S'il y a plus de 2 processeurs, on subdivise en deux comme pour le cas précédent, puis on résout chaque sous-problème avec la moitié des processeurs...
 
Une amélioration: si le nombre de processeur est impair (2n+1), un des sous-problème aura n processeurs et l'autre n+1; on peut donc viser un rapport de (n+1)/n pour le partitionnement au lieu de 1...
 
[edit]
petite correction, ça ne donne pas la solution optimale pour l'exemple 4, ça me donne 122, alors qu'il y a moyen de faire 120.


Message édité par dividee le 08-08-2005 à 05:13:07
n°1172910
xiluoc
un pc pour les unirs ....
Posté le 09-08-2005 à 05:53:34  profilanswer
 

jai implementer le brute force pour 2 cpu, ca marche maintenant faut que jessaye le deivide and conqueer.
je trouve moncode un peu lourd quand meme, quelques critique ?
 

Code :
  1. //case 2 cpu
  2. int partA = 0;
  3. int partB = 0;
  4. double bestRatioSofar = 0;
  5. double Ratio = 0;
  6. int sum = 0;
  7. int curentCut = 1;
  8. int maxLoad = 0;
  9. int bestCutPosition = 0;
  10. //row = true col = false;
  11. bool colOrRowCut = true;
  12. // do  rows
  13. // m-1 possibilities of arrangement  
  14. for (int i =0; i < rowSize -1  ; i++)
  15. {
  16.  for (int k = 0; k < curentCut; k++)
  17.  {
  18.   //cout << "part A include row " << k << endl;
  19.   for (int j = 0; j < colSize; j ++)
  20.   {
  21.    sum += matrix[k][j];
  22.   }
  23.   partA = sum;
  24.  }
  25.  sum = 0;
  26.  for (int k = curentCut  ; k < rowSize ; k++)
  27.  {
  28.   //cout << "part B include row " << k << endl;
  29.   for (int j = 0; j < colSize; j ++)
  30.   {
  31.    sum += matrix[k][j];
  32.   }
  33.   partB = sum;
  34.  }
  35.  if (partA > partB) Ratio = (double)partA /  (double)partB;
  36.  else  Ratio = (double)partB /  (double)partA;
  37.  //update best ratiosofar
  38.  if (Ratio < bestRatioSofar || bestRatioSofar == 0)
  39.  {
  40.   bestRatioSofar = Ratio;
  41.   colOrRowCut = true;
  42.   bestCutPosition = curentCut;
  43.   if (partA > partB) maxLoad = partA;
  44.   else maxLoad = partB;
  45.  }
  46.  //cout << " part A : " << partA << " part B : " << partB << " ratio " << Ratio << endl;
  47.  partA = 0;
  48.  partB = 0;
  49.  sum = 0;
  50.  curentCut++;
  51. }
  52. curentCut = 1;
  53. // do cols
  54. //n-1 possibilities of arrangement  
  55. for (int i =0; i < colSize -1  ; i++)
  56. {
  57.  for (int k = 0; k < curentCut; k++)
  58.  {
  59.  // cout << "part A include col " << k << endl;
  60.   for (int j = 0; j < rowSize; j ++)
  61.   {
  62.    sum += matrix[j][k];
  63.   }
  64.   partA = sum;
  65.  }
  66.  sum = 0;
  67.  for (int k = curentCut  ; k < rowSize ; k++)
  68.  {
  69.  // cout << "part B include col " << k << endl;
  70.   for (int j = 0; j < rowSize; j ++)
  71.   {
  72.    sum += matrix[j][k];
  73.   }
  74.   partB = sum;
  75.  }
  76.  if (partA > partB) Ratio = (double)partA /  (double)partB;
  77.  else  Ratio = (double)partB /  (double)partA;
  78.  //update best ratiosofar
  79.  if (Ratio < bestRatioSofar || bestRatioSofar == 0)
  80.  {
  81.   bestRatioSofar = Ratio;
  82.   colOrRowCut = false;
  83.   bestCutPosition = curentCut;
  84.   if (partA > partB) maxLoad = partA;
  85.   else maxLoad = partB;
  86.  }
  87.  //cout << " part A : " << partA << " part B : " << partB << " ratio " << Ratio << endl;
  88.  partA = 0;
  89.  partB = 0;
  90.  sum = 0;
  91.  curentCut++;
  92. }
  93. cout << "The maximum load (first line) followed by/nthe  " <<procNbr << " regions selected are (top row, left column, bottom row, right column):" << endl;
  94. cout << maxLoad << endl;


 :jap:

n°1173272
xiluoc
un pc pour les unirs ....
Posté le 09-08-2005 à 15:00:51  profilanswer
 

je pense avoir reussi a implementer divide and conquer pour un nombre pair de cpu , a lexemple 4 ma de coupe se fait de :
 
top row, left col, down row, right col
0 0 4 0
0 1 4 1
0 2 3 4
4 2 4 4
 
ton Algo trouve la meme chose ?
max load de 122
ca mas pris 200 lignes de code :/

n°1173593
dividee
Posté le 09-08-2005 à 21:13:09  profilanswer
 

La solution précise trouvée dépend sûrement de petits détails d'implémentation, mais la qualité de cette solution devrait rester la même en moyenne...
 
Pour l'exemple 4 j'obtiens ceci (j'ai numéroté les lignes/colonnes en partant de 1):
1 1 2 5 load: 10
3 1 4 5 load: 10
5 1 5 2 load: 2
5 3 5 5 load: 122
 
J'ai implémenté l'algo en python; le C j'en fais plus que si je suis obligé ;)

Code :
  1. def solve(mat, n):
  2.     rects = []
  3.     def _solve(n, rect):
  4.         if n == 1:
  5.             rects.append(rect)
  6.         else:
  7.             if rect[2] == rect[0] and rect[3] == rect[1]:
  8.                 # more than 1 processor for a single task ?
  9.                 for i in xrange(n-1):
  10.                     rects.append((0,0,0,0)) # all processors are idle
  11.                 rects.append(rect) # except one
  12.             else:
  13.                 half_n = n/2
  14.                 ratio = float(n - half_n) / half_n
  15.                 r1, r2 = partition(rect, ratio)
  16.                 _solve(n-half_n, r1)
  17.                 _solve(half_n, r2)
  18.     def partition(rect, ratio):
  19.         best_diff = 1e100   # some big float
  20.         top, left, bottom, right = rect
  21.         whole_rect_sum = sum_rect(rect)
  22.         # split by lines
  23.         sum1 = 0
  24.         for split in xrange(top, bottom):
  25.             r1 = (top, left, split, right)
  26.             r2 = (split+1, left, bottom, right)
  27.             sum1 += sum_rect((split,left,split,right))
  28.             sum2 = whole_rect_sum - sum1
  29.             if sum1 < sum2:
  30.                 r1, r2 = r2, r1
  31.                 r = float(sum2)/sum1
  32.             else:
  33.                 r = float(sum1)/sum2
  34.             if abs(r-ratio) < best_diff:
  35.                 best_diff = abs(r-ratio)
  36.                 best_r1 = r1
  37.                 best_r2 = r2
  38.         # split by columns
  39.         sum1 = 0
  40.         for split in xrange(left, right):
  41.             r1 = (top, left, bottom, split)
  42.             r2 = (top, split+1, bottom, right)
  43.             sum1 += sum_rect((top,split,bottom,split))
  44.             sum2 = whole_rect_sum - sum1
  45.             if sum1 < sum2:
  46.                 r1, r2 = r2, r1
  47.                 r = float(sum2)/sum1
  48.             else:
  49.                 r = float(sum1)/sum2
  50.             if abs(r-ratio) < best_diff:
  51.                 best_diff = abs(r-ratio)
  52.                 best_r1 = r1
  53.                 best_r2 = r2
  54.         return (best_r1, best_r2)
  55.     def sum_rect(r):
  56.         top, left, bottom, right = r
  57.         return sum(sum(line[left-1:right]) for line in mat[top-1:bottom])
  58.            
  59.     _solve(n, (1,1, len(mat), len(mat[0])))
  60.     loads = [sum_rect(r) for r in rects]
  61.     response = zip(loads, rects)
  62.     response.sort(reverse=True) # highest load first
  63.     return response


 
66 lignes, moins si je n'avais pas fait qqes optimisations. J'ai malheureusement écrasé la version non optimisée, mais les optimisations ont surtout pour but d'appeler moins souvent sum_rect; tu peux remplacer les calculs de sum1 et sum2 dans les boucles for de la fonction partition par sum1=sum_rect(r1) et sum2=sum_rect(r2) pour comprendre comment ça marche sans te laisser distraire par les optimisations. Je suis pas certain à 100% que c'est "bug free", mais j'ai quand-même prévu le cas où il essayerait de partager une tâche entre plusieurs processeurs.
 
Moins de 20 secondes pour une matrice de 1000x1000 et 10000 processeurs, mais qui a autant de processeurs à disposition ?  
 
Je n'ai pas lu ton code C (pardon, C++, mais ça ressemble à du C :p), mais à priori tu aurais intérêt à modulariser un peu en découpant l'algo en plusieurs fonctions...
 
Les tests:

Code :
  1. mat1 = ((1,2,3),)*3
  2. mat2 = (((1,)*4),)*4
  3. mat3 = (((1,)*4),)*3 + ((1,120,1,1),)
  4. mat4 = (((1,)*5),)*4 + ((1,1,120,1,1),)
  5. mat5 = ((1,120,1), (1,1,1), (1,1,1))
  6. tests = [ # (matrix, num_processors)
  7.     (mat1, 2),
  8.     (mat2, 2),
  9.     (mat3, 3),
  10.     (mat4, 4),
  11.     (mat5, 2),
  12.     ]
  13. print
  14. for idx, test in enumerate(tests):
  15.     print "Example", idx+1
  16.     print solve(*test)
  17. print "\nA more complex example"
  18. from random import seed,randint
  19. seed(0)
  20. mat_size = 20
  21. nproc = 20
  22. mat = [[randint(1,99) for i in xrange(mat_size)] for i in xrange(mat_size)]
  23. for line in mat:
  24.     for col in line:
  25.         print "%2d " % col,
  26.     print
  27. print "number of processors: ", nproc
  28. perfect = float(sum(sum(x) for x in mat)) / nproc
  29. print "perfect load (without constraints):", perfect
  30. proc_rects = solve(mat, nproc)
  31. for p in proc_rects:
  32.     print p


mood
Publicité
Posté le 09-08-2005 à 21:13:09  profilanswer
 

n°1173594
dividee
Posté le 09-08-2005 à 21:19:39  profilanswer
 

et l'output des tests (il y a pour chaque CPU, d'abord sa charge puis le rectangle dont il s'occupe)

Example 1
[(9, (1, 3, 3, 3)), (9, (1, 1, 3, 2))]
Example 2
[(8, (3, 1, 4, 4)), (8, (1, 1, 2, 4))]
Example 3
[(121, (4, 1, 4, 2)), (12, (1, 1, 3, 4)), (2, (4, 3, 4, 4))]
Example 4
[(122, (5, 3, 5, 5)), (10, (3, 1, 4, 5)), (10, (1, 1, 2, 5)), (2, (5, 1, 5, 2))]
Example 5
[(122, (1, 1, 1, 3)), (6, (2, 1, 3, 3))]
 
A more complex example
84  76  42  26  51  41  78  31  48  58  90  50  28  75  62  25  91  98  81  90  
31  73  89  68  47  10  43  61  91  96  48  86  26  80  55   2  72  40  82  67  
 1  49  86  25  33  87  19  57  24  96  80  45   8  32  51  93  11  55  70  55  
81  54  96  60  59  45  60  39  57  29  19  19  61  66  48   9  76  87  92  84  
89  92  54  39  70  28  81  85  89  59  95  58  45  66  99  91  79   9  61  49  
63  84  25  73  12  22  79  33  81  10  15  70   5  57  91  53  68   3  63  61  
58  39  37  98   4   3  96  19  13  21  80  93   3  43  11  26  22  65  35  18  
50   4  10  98  20  36  73  83  91  17  67  96   6  67  84  34  25  60  44  18  
47  41  57  51  31  36  83  25  56   2  74  34   5  28  24  95  35  29  36  94  
63  62  71  39  42  65   1  20  34  24  64  38  87  57  42  40  70  42  66   5  
45  26  16  53  49  56  75  88  49  31  47  81  87  81  19  99  63   9  72  98  
40  68  32  22  72   1  82  53  10  12  65  87  28  97  10  85  40   9  28  45  
79  86  14  52  65  35  87  28   2   5  68  56  94  93  91   5  75  70  65  71  
90  64  37  54  21  59   1  15  34  79  72  34  62   5  17  98  29  40  55  30  
48  24   5  18  52   8  40  33  42  10  90  47  84  97  35  48  70  43  30  73  
89  92  63  38  97  64   7   9  75   7   1  39  52  45  49  58  68  42  37  98  
26  77  43  36   7  86  70  90  45  68  12  40  21   5  94  22  15  20  38  55  
15  98  98  15  41  68  87  50  91  32  50  50  67  20  61  22  34  96  90  81  
 4  15  26  78  84  58  72  80   7   9  87   4  23   5   2  84  33  16  15  65  
96  50  90  50  57  68  80  76  99  74  90  21  54  60  82  48  79  39  59  85  
number of processors:  20
perfect load (without constraints): 1027.6
(1122, (4, 15, 10, 17))
(1100, (1, 15, 3, 20))
(1099, (16, 1, 20, 4))
(1077, (1, 11, 5, 14))
(1066, (1, 7, 10, 8))
(1065, (16, 17, 20, 20))
(1062, (7, 1, 10, 6))
(1052, (16, 8, 20, 11))
(1046, (4, 1, 6, 6))
(1038, (11, 12, 12, 20))
(1028, (16, 12, 20, 16))
(1021, (4, 18, 10, 20))
(1015, (13, 1, 15, 8))
(996, (1, 9, 10, 10))
(992, (11, 1, 12, 11))
(989, (6, 11, 10, 14))
(974, (13, 9, 15, 14))
(946, (16, 5, 20, 7))
(945, (13, 15, 15, 20))
(919, (1, 1, 3, 6))

n°1173761
xiluoc
un pc pour les unirs ....
Posté le 10-08-2005 à 10:57:46  profilanswer
 

convertion c++ reussi =) 200 lignes, vive python.
par contre 22.5 s en python
et 3 a peine en  c++ ?!
je comprend pas.
voila le code

Code :
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdio.h>
  4. using namespace std;
  5. struct Rect {
  6.     int rowTop;
  7.     int colLeft;
  8.     int rowDown;
  9.     int colRight;
  10. };
  11. vector <Rect> subRectV;
  12. vector <Rect>::iterator it;
  13. void solve (int procNbr, int startRow, int startCol, int endRow, int endCol);
  14. void partition (Rect R , float ratio, Rect &  r1, Rect & r2);
  15. int sum_rect(Rect R);
  16. int **matrix = NULL;
  17. int main()
  18. {
  19. int procNbr, rowSize, colSize;
  20. //get the numbers of proccesors
  21. cout << "Enter the number of processors that need to share the load: ";
  22. ///cin >> procNbr;
  23. //get the size of the matrix
  24. cout << "Enter the number of rows and columns of the matrix of the loads: ";
  25. //cin >> rowSize >> colSize;  
  26. //create the matrix dynamicaly
  27. rowSize =1000;
  28. colSize = 1000;
  29. //Allocate an array of size rowSize.
  30. matrix = new int*[rowSize];
  31. //Loop through all integer pointers and allocate each one an array of size colSize.
  32. for (int i = 0; i < rowSize; i++)
  33.  matrix[i] = new int[colSize];
  34. //Input the values of the matrix
  35. cout << "\nEnter the load matrix (row by row, left to right): \n";
  36. //for (int i=0; i < rowSize; i++)
  37.  //for (int j=0; j < colSize; j++)
  38.   //cin >> matrix[i][j];
  39. //cout << "\nThe maximum load (first line) followed by\nthe 2 regions selected are (top row, left column, bottom row, right column):"<< endl;
  40. for (int i = 0; i < rowSize; i++) {
  41.  for (int j = 0; j < colSize; j++) {
  42.          matrix[i][j] = (rand()%999)+1;
  43.      }
  44.  
  45.     }
  46.     procNbr = 10000;
  47.     clock_t start, finish;
  48.  
  49.   long int duration;
  50.     start = clock ();
  51.    
  52.    
  53.     solve (procNbr, 1, 1, rowSize , colSize);
  54. int maxLoad = 0;
  55. cout << "The maximum load (first line) followed by\nthe  " << procNbr
  56. << " regions selected are (top row, left column, bottom row, right column):" << endl;
  57. for (it =  subRectV.begin() ; it !=  subRectV.end(); it++) {
  58.  int load =  sum_rect(*it);
  59.  if (load > maxLoad) { maxLoad = load; }
  60.  cout << maxLoad << endl;
  61.  finish = clock ();
  62.       duration = finish - start;
  63.       printf ("Actual:%ld\n", duration);
  64.       Rect r;
  65.      r.rowTop = 1;
  66.  r.colLeft = 1;
  67.  r.rowDown = rowSize;
  68.  r.colRight = colSize;
  69.       cout << "perfect : " << sum_rect(r)/procNbr << endl;
  70. //for (it =  subRectV.begin() ; it !=  subRectV.end(); it++) {
  71. // cout << it->rowTop -1 << " " << it->colLeft -1<< " " << it->rowDown -1<< " " << it->colRight -1 << endl;
  72. //}   
  73. //finish, free memory
  74. for (int i = 0; i < rowSize; i++)
  75.  delete[] matrix[i];
  76. delete[] matrix;
  77. matrix = NULL;
  78. return 0;
  79. }
  80. void solve (int procNbr, int startRow, int startCol, int endRow, int endCol)
  81. {
  82. if (procNbr == 1) {
  83.  Rect rTmp;
  84.  rTmp.rowTop = startRow;
  85.  rTmp.colLeft = startCol;
  86.  rTmp.rowDown = endRow;
  87.  rTmp.colRight = endCol;
  88.  subRectV.push_back(rTmp);
  89. }
  90. else {
  91.  int half_procNbr = procNbr / 2;
  92.  float ratio = (float)  (procNbr - half_procNbr) / half_procNbr;
  93.  Rect r1, r2;
  94.  Rect R;
  95.  R.rowTop = startRow;
  96.  R.colLeft = startCol;
  97.  R.rowDown = endRow;
  98.  R.colRight = endCol;
  99.  partition(R, ratio, r1, r2);
  100.  solve(procNbr - half_procNbr, r1.rowTop, r1.colLeft , r1.rowDown , r1.colRight);
  101.  solve(half_procNbr , r2.rowTop , r2.colLeft , r2.rowDown , r2.colRight );
  102. }
  103. }
  104. void partition (Rect R, float ratio, Rect & best_r1, Rect & best_r2)
  105. {
  106. float r;
  107. int sum1 = 0;
  108. int sum2 = 0;
  109. float best_diff = 1e100;  // some big float
  110. int top = R.rowTop;
  111. int left = R.colLeft;
  112. int bottom = R.rowDown;
  113. int right = R.colRight;
  114. int whole_rect_sum = sum_rect(R);
  115. // split by lines
  116. sum1 = 0;
  117. //n-1 rows of possible combinations
  118. for (int split = top ; split < bottom ; split++) {
  119.  Rect r1;
  120.  r1.rowTop = top;
  121.  r1.colLeft = left;
  122.  r1.rowDown = split;
  123.  r1.colRight = right;
  124.  Rect r2;
  125.  r2.rowTop = split+1;
  126.  r2.colLeft = left;
  127.  r2.rowDown = bottom;
  128.  r2.colRight = right;
  129.  Rect rTmp;
  130.  rTmp.rowTop = split;
  131.  rTmp.colLeft = left;
  132.  rTmp.rowDown = split;
  133.  rTmp.colRight = right;
  134.  sum1 += sum_rect(rTmp);
  135.  sum2 = whole_rect_sum - sum1;
  136.  if (sum1 < sum2) {
  137.    swap(r1, r2);
  138.    r = float(sum2)/sum1;
  139.  }
  140.  else {
  141.    r = float(sum1)/sum2;
  142.  }
  143.  if (abs(r-ratio) < best_diff) {
  144.   best_diff = abs(r-ratio);
  145.   best_r1 = r1;
  146.   best_r2 = r2;
  147.  }
  148. }
  149. // split by columns
  150. sum1 = 0;
  151. //m-1 cols of possible combinations
  152. for (int split = left ; split < right ; split++) {
  153.  Rect r1;
  154.  r1.rowTop = top;
  155.  r1.colLeft = left;
  156.  r1.rowDown = bottom;
  157.  r1.colRight = split;
  158.  Rect r2;
  159.  r2.rowTop = top;
  160.  r2.colLeft = split+1;
  161.  r2.rowDown = bottom;
  162.  r2.colRight = right;
  163.  Rect rTmp;
  164.  rTmp.rowTop = top;
  165.  rTmp.colLeft = split;
  166.  rTmp.rowDown = bottom;
  167.  rTmp.colRight = split;
  168.  sum1 += sum_rect(rTmp);
  169.  sum2 = whole_rect_sum - sum1;
  170.  if (sum1 < sum2) {
  171.    swap(r1, r2);
  172.    r = float(sum2)/sum1;
  173.  }
  174.  else {
  175.    r = float(sum1)/sum2;
  176.  }
  177.  if (abs(r-ratio) < best_diff) {
  178.   best_diff = abs(r-ratio);
  179.   best_r1 = r1;
  180.   best_r2 = r2;
  181.  }
  182. }
  183. }
  184. int sum_rect(Rect R)
  185. {
  186. int sum=0;
  187. int top = R.rowTop;
  188. int left = R.colLeft;
  189. int bottom = R.rowDown;
  190. int right = R.colRight;
  191. for (int i= top-1; i < bottom ; i++) {
  192.  for (int j= left-1; j < right ; j++) {
  193.   sum += matrix[i][j];
  194.  }
  195. }
  196. return sum;
  197. }


Message édité par xiluoc le 10-08-2005 à 10:58:29
n°1174212
dividee
Posté le 10-08-2005 à 21:11:10  profilanswer
 

xiluoc a écrit :

convertion c++ reussi =) 200 lignes, vive python.
par contre 22.5 s en python
et 3 a peine en  c++ ?!
je comprend pas.


Qu'est-ce que ça a d'étonnant ? Entre un langage dynamiquement typé et essentiellement interprété, et un langage statiquement typé et compilé (et pour lequel les compilateurs ont une certaine maturité), ça me parait tout à fait crédible comme ordre de grandeur...


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Algo

  Repartition decpu load "decoupe de rectangle", et C++

 

Sujets relatifs
[c#] declarer la connection avant le page loadpb avec "load data infile"
[GD] Rendre une image transparente + ajout d'un rectangle.Ajouter un certain nombre de composants lors du load de l'etat
opengl, rectangle contour / couleur de remplissageCmt savoir si PHP a bien load MySQL ?!
[Algo][Java] Optimiser la répartition d'un algoDecoupe d une image
Font / GlyphVector : étirer une chaîne dans un rectangleles ETL (extract, transform and load)
Plus de sujets relatifs à : Repartition decpu load "decoupe de rectangle", et C++


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR