java - Performance of nested loop vs hard coded matrix multiplication -


i reading book on 2d game programming , being walked through 3x3 matrix class linear transformations. author has written method multiplying 2 3x3 matrices follows.

public matrix3x3f mul(matrix3x3f m1) {     return new matrix3x3f(new float[][]     {         {               this.m[0][0] * m1.m[0][0]     // m[0,0]             + this.m[0][1] * m1.m[1][0]             + this.m[0][2] * m1.m[2][0],               this.m[0][0] * m1.m[0][1]     // m[0,1]             + this.m[0][1] * m1.m[1][1]             + this.m[0][2] * m1.m[2][1],               this.m[0][0] * m1.m[0][2]     // m[0,2]             + this.m[0][1] * m1.m[1][2]             + this.m[0][2] * m1.m[2][2],         },         {               this.m[1][0] * m1.m[0][0]     // m[1,0]             + this.m[1][1] * m1.m[1][0]             + this.m[1][2] * m1.m[2][0],               this.m[1][0] * m1.m[0][1]     // m[1,1]             + this.m[1][1] * m1.m[1][1]             + this.m[1][2] * m1.m[2][1],               this.m[1][0] * m1.m[0][2]     // m[1,2]             + this.m[1][1] * m1.m[1][2]             + this.m[1][2] * m1.m[2][2],         },         {               this.m[2][0] * m1.m[0][0]     // m[2,0]             + this.m[2][1] * m1.m[1][0]             + this.m[2][2] * m1.m[2][0],               this.m[2][0] * m1.m[0][1]     // m[2,1]             + this.m[2][1] * m1.m[1][1]             + this.m[2][2] * m1.m[2][1],               this.m[2][0] * m1.m[0][2]     // m[2,2]             + this.m[2][1] * m1.m[1][2]             + this.m[2][2] * m1.m[2][2],         },     }); } 

if needed write method same have come nested loop did of these calculations automatically, assuming perhaps author has written out way people little math background can follow along easier.

does sound fair assumption or nested loop version of method possibly cause performance issues when used heavily in loop performance vital?

from looks of it, think it's clarity's sake, not performance's sake. consider fact it's java code. there's object allocation in return statement. if performance critical conditional jump of for-loop can't afforded, result written mutable instance.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -