Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include "routines.h"
  2. void generate_matrix_auto(int rows,int cols,matr_t matr,int* non_zero_els,int *percent)
  3. {
  4.  
  5. *percent=rand()%100;
  6. int non_zero=rows*cols*(*percent)/100;//кол-во ненулевых элементов
  7. *non_zero_els=non_zero;
  8. while(non_zero)
  9. {
  10. int rand_i= rand()%rows;
  11. int rand_j= rand()%cols;
  12. int rand_value=(1+rand()%100);
  13. if (!matr[rand_i][rand_j])
  14. {
  15. matr[rand_i][rand_j]=rand_value;
  16. non_zero--;
  17. }
  18.  
  19. }
  20. }
  21. void test(int rows,int cols)
  22. {
  23. matr_t matr1=NULL,matr2=NULL,matr3=NULL;
  24.  
  25. arr_t A1=NULL,IA1=NULL,JA1=NULL;
  26. arr_t A2=NULL,IA2=NULL,JA2=NULL;
  27. arr_t A3=NULL,IA3=NULL,JA3=NULL;
  28.  
  29. int percent_1;
  30. int percent_2;
  31.  
  32. int non_zero_els_1;
  33. int non_zero_els_2;
  34. int non_zero_els_3;
  35.  
  36. create_and_zero_matr(&matr1,rows,cols);
  37. create_and_zero_matr(&matr2,rows,cols);
  38. create_and_zero_matr(&matr3,rows,cols);
  39.  
  40.  
  41. generate_matrix_auto(rows,cols,matr1,&non_zero_els_1,&percent_1);
  42. generate_matrix_auto(rows,cols,matr2,&non_zero_els_2,&percent_2);
  43.  
  44. sum_simple_matrs(matr1, matr2, matr3, rows, cols);
  45. non_zero_els_3=count_non_zero(matr3,rows,cols);
  46. create_arrs(&A1,&IA1,&JA1,non_zero_els_1,cols);
  47. create_arrs(&A2,&IA2,&JA2,non_zero_els_2,cols);
  48. create_arrs(&A3,&IA3,&JA3,non_zero_els_3,cols);
  49.  
  50.  
  51. reconstruct_matr(matr1,rows,cols,A1,IA1,JA1,non_zero_els_1 );
  52. reconstruct_matr(matr2,rows,cols,A2,IA2,JA2,non_zero_els_2 );
  53.  
  54. time_t timer_beg = clock();
  55. sum_simple_matrs(matr1, matr2, matr3, rows, cols);
  56. time_t timer_end = clock();
  57. int timer_1 = (timer_end-timer_beg);
  58. timer_beg = clock();
  59. sum_three_arrs_matrs(A1,IA1,JA1,A2,IA2,JA2,A3,IA3,JA3,rows,cols);
  60. timer_end = clock();
  61. int timer_2 = (timer_end-timer_beg);
  62.  
  63. printf("\nRows - %d , columns - %d\n",rows,cols);
  64. printf ("Matrix %d is full on %d%%\n",1,percent_1);
  65. printf ("Matrix %d is full on %d%%\n",2,percent_2);
  66. puts ("Usual calculation :");
  67. if (timer_1 == 0)
  68. printf ("Needed time is under %d ms\n",timer_1);
  69. else
  70. printf ("Needed time : %dms\n",timer_1);
  71. printf ("Needed memory: %" PRId64 " bytes\n",3*sizeof(int)*rows*cols);
  72. puts ("\nSparse calculation :");
  73. if (timer_2 == 0)
  74. printf ("Needed time is under %d ms\n",timer_2);
  75. else
  76. printf ("Needed time : %d ms\n",timer_2);
  77.  
  78. int size1 = 2*non_zero_els_1 + cols+1;
  79. int size2 = 2*non_zero_els_2 + cols+1;
  80. int size3 = 2*non_zero_els_3 + cols+1;
  81.  
  82. printf ("Needed memory: %" PRId64 " bytes\n",sizeof(int)*(size1+size2+size3));
  83.  
  84. free_three_arrs( A1,IA1,JA1);
  85. free_three_arrs(A2,IA2,JA2);
  86. free_three_arrs(A3,IA3,JA3);
  87.  
  88. free_matr(matr1, rows);
  89. free_matr(matr2, rows);
  90. free_matr(matr3, rows);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement