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){}
}