I'm trying to learn how to optimize my c code, so I found some articles on the internet and remade my function so that it should execute faster. And when I compile it without optimization flags it works (second function is about 12% faster than first), but when I use it with gcc -O3 then second function is much slower (about 50%). Do you have any idea why is that? Thanks for any help.
First function:
typedef struct {
double *data;
int rows;
int columns;
} Matrix;
Matrix *matrixMultiplication(Matrix *a, Matrix *b) {
if(a->columns != b->rows)
return NULL;
Matrix *matrix = createMatrix(a->rows, b->columns);
set(0, matrix);
for(int i = 0; i < matrix->rows; i++) {
for(int j = 0; j < a->columns; j++) {
for(int k = 0; k < b->columns; k++) {
matrix->data[i * matrix->columns + k] += a->data[i * a->columns + j] * b->data[j * b->columns + k];
}
}
}
return matrix;
}
Second function:
typedef struct {
float *data;
unsigned int rows;
unsigned int columns;
} Matrix_2;
unsigned int matrixMultiplication_2(Matrix_2 *a, Matrix_2 *b, Matrix_2 **c) {
Matrix_2 *matrix;
if(a->columns != b->rows)
return 0;
createMatrix_2(a->rows, b->columns, &matrix);
set_2(0, matrix);
for(unsigned int i = matrix->rows; i--;) {
for(unsigned int j = a->columns; j--;) {
for(unsigned int k = b->columns; k--;) {
matrix->data[i * matrix->columns + k] += a->data[i * a->columns + j] * b->data[j * b->columns + k];
}
}
}
*c = matrix;
return 1;
}