0

i realize a verry large matrix performance between two codes

Code 1:

#include <stdio.h>
struct pix{
    unsigned int r,g,b;
};
#define TAM 10000
struct pix color[TAM][TAM];
void main(){
    int i,j;
    for(i=0; i<TAM;i++)
        for(j=0;j<TAM;j++){
            color[i][j].r=
            (
                color[i][j].r+
                color[i][j].g+
                color[i][j].b
            ) / 3;
        }
}

Code2:

 #include <stdio.h>
struct pix{
    unsigned int r,g,b;
};
#define TAM 10000
struct pix color[TAM][TAM];
void main(){
    int i,j;
    for(i=0; i<TAM;i++)
        for(j=0;j<TAM;j++){
            color[i][j].r=
            (
                color[j][i].r+
                color[j][i].g+
                color[j][i].b
            ) / 3;
        }
}

The only difference between both is the index order(ij and ji) and they have a large performance difference. Why this happen ?

Code 1 performance

Code 2 performance

After a some runs, the avarage of time is:

Code1: 1.541s

Code2: 2.592s

  • I noticed a very large matrix performance difference between two codes** – Joao Augusto Nov 06 '18 at 21:38
  • Possible duplicate of [Why is the performance of these matrix multiplications so different?](https://stackoverflow.com/questions/4029138/why-is-the-performance-of-these-matrix-multiplications-so-different) – EOF Nov 06 '18 at 21:39
  • Better duplicate: https://stackoverflow.com/q/9936132/3185968 – EOF Nov 06 '18 at 21:41
  • 2
    First code accesses one element per loop, the 2nd accesses two different elements per loop. Why does a 70% slow down seem unusual? – chux - Reinstate Monica Nov 06 '18 at 21:45

0 Answers0