Time of execution cannot be correctly measured.

Hello Dear All

I'm writing a code to do matrices calculation where i'm having trouble measuring the time of execution.
If i only do the matrices calculation, the time couldn't be recorded correctly.
However, after the time printing, if i choose to print at least one cell of the array, the execution time of the calculation increases to a value.
For 1k or 100k calculation cycles, the time is always 4us which cannot be right.
After i enable the print code in red below, the time shows linearity vs the number of execution cycles.

I have also printed out the whole result array and they show the correct values.
Please help.

Thanks in advance.

Please see below Code:

byte i;    //array calculation index
byte j;
unsigned long a;

int mX[13][13];  
int mY[13][13];
long mZ[13][13];
unsigned long time_start;  //calculation time record
unsigned long time_end;
int sum=0;


void setup() {
Serial.begin(9600);
}

void loop() {
//Generate two 13x13 random matrices------------------------------------------------
for(i=0;i<13;i++)
{
  for(j=0;j<13;j++)
  { 
    mX[i][j]  = random(-128,127);
    mY[i][j]  = random(-128,127);
  }
}


  time_start=millis();  //startintg time--------------------------------------------

  for(a = 0; a < 1000; a++)
  { 
        for(i = 0; i < 13; i++)
        {
              for(j = 0; j < 13; j++)
              {
                     mZ[i][j] =2*mX[i][j]+mX[i][0]*mY[0][j]+ mX[i][1]* mY[1][j]+ mX[i][2]* mY[2][j]+ mX[i][3]* mY[3][j]+ mX[i][4]* mY[4][j]+ mX[i][5]* mY[5][j]+ mX[i][6]* mY[6][j]+ mX[i][7]* mY[7][j]+ mX[i][8]* mY[8][j]+ mX[i][9]* mY[9][j]+ mX[i][10]* mY[10][j]+ mX[i][11]* mY[11][j]+ mX[i][12]* mY[12][j]; //Z=2X+XY
              }

        }
   }
  time_end=millis();    //end time
  Serial.println((time_end - time_start));  //execution time

  //Serial.print(mZ[0][0]); [color=red]//!!!!!!!!!!!!! If i enable this line of code, the measured time changes.[/color]
   

  while(Serial.available()==0){}
  }

...and that's why we ask you to please remember to use code tags when posting code

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

...R

Consider contacting your classmate to discuss the assignment.

the time is always 4us which cannot be right.

You found that out by subtracting two millis values?

The compiler is optimizing the entire calculation out of existence because it has detected that the results of the calculation aren't being used - except when you print a result.
Try declaring the mZ array to be volatile which should make the compiler leave it alone.

volatile long mZ[13][13];

Pete

el_supremo:
The compiler is optimizing the entire calculation out of existence because it has detected that the results of the calculation aren't being used - except when you print a result.
Try declaring the mZ array to be volatile which should make the compiler leave it alone.

volatile long mZ[13][13];

Pete

Thanks a lot Pete.
This is indeed the problem where if i never use the matrix value again after calculation, the compiler just skipped the calculation.

Kind regards