Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "routines.h"
- void generate_matrix_auto(int rows,int cols,matr_t matr,int* non_zero_els,int *percent)
- {
- *percent=rand()%100;
- int non_zero=rows*cols*(*percent)/100;//кол-во ненулевых элементов
- *non_zero_els=non_zero;
- while(non_zero)
- {
- int rand_i= rand()%rows;
- int rand_j= rand()%cols;
- int rand_value=(1+rand()%100);
- if (!matr[rand_i][rand_j])
- {
- matr[rand_i][rand_j]=rand_value;
- non_zero--;
- }
- }
- }
- void test(int rows,int cols)
- {
- matr_t matr1=NULL,matr2=NULL,matr3=NULL;
- arr_t A1=NULL,IA1=NULL,JA1=NULL;
- arr_t A2=NULL,IA2=NULL,JA2=NULL;
- arr_t A3=NULL,IA3=NULL,JA3=NULL;
- int percent_1;
- int percent_2;
- int non_zero_els_1;
- int non_zero_els_2;
- int non_zero_els_3;
- create_and_zero_matr(&matr1,rows,cols);
- create_and_zero_matr(&matr2,rows,cols);
- create_and_zero_matr(&matr3,rows,cols);
- generate_matrix_auto(rows,cols,matr1,&non_zero_els_1,&percent_1);
- generate_matrix_auto(rows,cols,matr2,&non_zero_els_2,&percent_2);
- sum_simple_matrs(matr1, matr2, matr3, rows, cols);
- non_zero_els_3=count_non_zero(matr3,rows,cols);
- create_arrs(&A1,&IA1,&JA1,non_zero_els_1,cols);
- create_arrs(&A2,&IA2,&JA2,non_zero_els_2,cols);
- create_arrs(&A3,&IA3,&JA3,non_zero_els_3,cols);
- reconstruct_matr(matr1,rows,cols,A1,IA1,JA1,non_zero_els_1 );
- reconstruct_matr(matr2,rows,cols,A2,IA2,JA2,non_zero_els_2 );
- time_t timer_beg = clock();
- sum_simple_matrs(matr1, matr2, matr3, rows, cols);
- time_t timer_end = clock();
- int timer_1 = (timer_end-timer_beg);
- timer_beg = clock();
- sum_three_arrs_matrs(A1,IA1,JA1,A2,IA2,JA2,A3,IA3,JA3,rows,cols);
- timer_end = clock();
- int timer_2 = (timer_end-timer_beg);
- printf("\nRows - %d , columns - %d\n",rows,cols);
- printf ("Matrix %d is full on %d%%\n",1,percent_1);
- printf ("Matrix %d is full on %d%%\n",2,percent_2);
- puts ("Usual calculation :");
- if (timer_1 == 0)
- printf ("Needed time is under %d ms\n",timer_1);
- else
- printf ("Needed time : %dms\n",timer_1);
- printf ("Needed memory: %" PRId64 " bytes\n",3*sizeof(int)*rows*cols);
- puts ("\nSparse calculation :");
- if (timer_2 == 0)
- printf ("Needed time is under %d ms\n",timer_2);
- else
- printf ("Needed time : %d ms\n",timer_2);
- int size1 = 2*non_zero_els_1 + cols+1;
- int size2 = 2*non_zero_els_2 + cols+1;
- int size3 = 2*non_zero_els_3 + cols+1;
- printf ("Needed memory: %" PRId64 " bytes\n",sizeof(int)*(size1+size2+size3));
- free_three_arrs( A1,IA1,JA1);
- free_three_arrs(A2,IA2,JA2);
- free_three_arrs(A3,IA3,JA3);
- free_matr(matr1, rows);
- free_matr(matr2, rows);
- free_matr(matr3, rows);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement