T
Taygun Kekec
Hello , I have been curious about the speed of 2 version of a matris
filling program and got suprised by the results.
Actually , I am having Data Structures lecture on university though
our professor claimed "using IF structures are costly , so you better
use immediate data addressing " so he gave us the pseudo codes that i
implemented in language C.
Here are the source codes :
/* Optimized.c * /
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ELEMENTS 15000
#define KOSEGEN 1
#define UPPER 2
#define LOWER 3
int main()
{
int i,j;
int **A;
/* 15000 x 15000 lik matris icin gerekli bellek allokasyonu */
A = (int **) malloc( ELEMENTS * sizeof(int*) );
for(i = 0; i < ELEMENTS; i++){
if ( (A = (int * )malloc( ELEMENTS * sizeof(int) )) == NULL ){
printf(" yer yok \n");
return -1;
}
}
time_t start,end;
time (&start); /* gerekli sistem saatini cekme */
for(i=0;i<ELEMENTS-1;i++){
A = KOSEGEN;
for(j= i+1;j<ELEMENTS;j++) {
A[j]= UPPER;
A[j]= LOWER;
}
}
A[ELEMENTS-1][ELEMENTS-1] = 1;
time (&end); /* gerekli sistem saatini cekme */
double dif = difftime (end,start); /* gecen sureyi dif degiskenine
atma */
printf ("%.4lf seconds for optimized algo.\n", dif );
free(A);
scanf("%d");
}
/* Optimized.c * /
----------------------
/* Unoptimized.c * /
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ELEMENTS 15000
#define KOSEGEN 1
#define UPPER 2
#define LOWER 3
int main()
{
int i,j;
int **A;
/* 15000 x 15000 lik matris icin gerekli bellek allokasyonu */
A = (int **) malloc( ELEMENTS * sizeof(int*) );
for(i = 0; i < ELEMENTS; i++){
if ( (A = (int * )malloc( ELEMENTS * sizeof(int) )) == NULL ){
printf(" yer yok \n");
return -1;
}
}
time_t start,end;
time (&start); /* gerekli sistem saatini cekme */
for(i=0;i<ELEMENTS;i++){
for(j=0;j<ELEMENTS;j++) {
if ( i<j)
A[j] = UPPER;
else if ( i>j)
A[j] = LOWER;
else
A[j] = KOSEGEN;
}
}
time (&end); /* gerekli sistem saatini cekme */
double dif = difftime (end,start); /* gecen sureyi dif
degiskenine atma */
printf ("%.4lf seconds for unoptimized algo.\n", dif );
free(A);
scanf("%d");
}
/* Unoptimized.c * /
Whats the reason for this ? Processing unit prefetching or something
about GCC ?
filling program and got suprised by the results.
Actually , I am having Data Structures lecture on university though
our professor claimed "using IF structures are costly , so you better
use immediate data addressing " so he gave us the pseudo codes that i
implemented in language C.
Here are the source codes :
/* Optimized.c * /
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ELEMENTS 15000
#define KOSEGEN 1
#define UPPER 2
#define LOWER 3
int main()
{
int i,j;
int **A;
/* 15000 x 15000 lik matris icin gerekli bellek allokasyonu */
A = (int **) malloc( ELEMENTS * sizeof(int*) );
for(i = 0; i < ELEMENTS; i++){
if ( (A = (int * )malloc( ELEMENTS * sizeof(int) )) == NULL ){
printf(" yer yok \n");
return -1;
}
}
time_t start,end;
time (&start); /* gerekli sistem saatini cekme */
for(i=0;i<ELEMENTS-1;i++){
A = KOSEGEN;
for(j= i+1;j<ELEMENTS;j++) {
A[j]= UPPER;
A[j]= LOWER;
}
}
A[ELEMENTS-1][ELEMENTS-1] = 1;
time (&end); /* gerekli sistem saatini cekme */
double dif = difftime (end,start); /* gecen sureyi dif degiskenine
atma */
printf ("%.4lf seconds for optimized algo.\n", dif );
free(A);
scanf("%d");
}
/* Optimized.c * /
----------------------
/* Unoptimized.c * /
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ELEMENTS 15000
#define KOSEGEN 1
#define UPPER 2
#define LOWER 3
int main()
{
int i,j;
int **A;
/* 15000 x 15000 lik matris icin gerekli bellek allokasyonu */
A = (int **) malloc( ELEMENTS * sizeof(int*) );
for(i = 0; i < ELEMENTS; i++){
if ( (A = (int * )malloc( ELEMENTS * sizeof(int) )) == NULL ){
printf(" yer yok \n");
return -1;
}
}
time_t start,end;
time (&start); /* gerekli sistem saatini cekme */
for(i=0;i<ELEMENTS;i++){
for(j=0;j<ELEMENTS;j++) {
if ( i<j)
A[j] = UPPER;
else if ( i>j)
A[j] = LOWER;
else
A[j] = KOSEGEN;
}
}
time (&end); /* gerekli sistem saatini cekme */
double dif = difftime (end,start); /* gecen sureyi dif
degiskenine atma */
printf ("%.4lf seconds for unoptimized algo.\n", dif );
free(A);
scanf("%d");
}
/* Unoptimized.c * /
Whats the reason for this ? Processing unit prefetching or something
about GCC ?