# some operation in loop.

• April 2nd, 2014, 05:00 AM
yosilev
some operation in loop.
hi friends ::o

i build some finite element in java.
i try to optimize my running time.
i do some double loop and use inside the loop in if else statement and also in switch case .

the loop is very long, sometimes become ~500 X 500.

you think that if i avoid from the if statement and the switch case inside the loop i will improve the time calculation by at least 10%?

there is something that i must to avoid ?

Code Java:

``` // Computing stiffness matrix. switch (materialType) { case ISOTROPIC: // Computing stiffness matrix for ISOTROPIC case. for (int row = 0, nvfi2 = nvfi * 2, index = 0, fieldsNvf = fields * nvfi; row < fieldsNvf; row++) { if (row == nvfi) { leftZeroMatrix = 3; } else if (row == nvfi2) { leftZeroMatrix = 6; } rightZeroMatrix = 0; for (int column = row; column < fieldsNvf; column++) {   if (nvfi2 <= column) { rightZeroMatrix = 2; } else if (nvfi <= column) { rightZeroMatrix = 1; }   materialMatrixIndex = leftZeroMatrix + rightZeroMatrix;   switch (materialMatrixIndex) { case 0: shapeFunctionI = row; shapeFunctionJ = column; break; case 1: shapeFunctionI = row; shapeFunctionJ = column - nvfi; break; case 2: shapeFunctionI = row; shapeFunctionJ = column - nvfi2; break; case 4: shapeFunctionI = row - nvfi; shapeFunctionJ = column - nvfi; break; case 5: shapeFunctionI = row - nvfi; shapeFunctionJ = column - nvfi2; break; case 8: shapeFunctionI = row - nvfi2; shapeFunctionJ = column - nvfi2; break; }   index = row + column * (column + 1) / 2;   // get the K(row, column) in stifness matrix. switch (materialMatrixIndex) { case 0: for (gaussPoint = 0; gaussPoint < ngp3; gaussPoint++) { stiffnessMatrixAsVector[index] += materailMatrixAsVectorDv[gaussPoint][0] * bMatrix[shapeFunctionI][gaussPoint][0] * bMatrix[shapeFunctionJ][gaussPoint][0] + materailMatrixAsVectorDv[gaussPoint][9] * (bMatrix[shapeFunctionI][gaussPoint][1] * bMatrix[shapeFunctionJ][gaussPoint][1] + bMatrix[shapeFunctionI][gaussPoint][2] * bMatrix[shapeFunctionJ][gaussPoint][2]); } break; case 1: for (gaussPoint = 0; gaussPoint < ngp3; gaussPoint++) { stiffnessMatrixAsVector[index] += materailMatrixAsVectorDv[gaussPoint][9] * bMatrix[shapeFunctionI][gaussPoint][1] * bMatrix[shapeFunctionJ][gaussPoint][0] + materailMatrixAsVectorDv[gaussPoint][1] * bMatrix[shapeFunctionI][gaussPoint][0] * bMatrix[shapeFunctionJ][gaussPoint][1]; } break; case 2: for (gaussPoint = 0; gaussPoint < ngp3; gaussPoint++) { stiffnessMatrixAsVector[index] += materailMatrixAsVectorDv[gaussPoint][20] * bMatrix[shapeFunctionI][gaussPoint][2] * bMatrix[shapeFunctionJ][gaussPoint][0] + materailMatrixAsVectorDv[gaussPoint][3] * bMatrix[shapeFunctionI][gaussPoint][0] * bMatrix[shapeFunctionJ][gaussPoint][2]; } break; case 4: for (gaussPoint = 0; gaussPoint < ngp3; gaussPoint++) { stiffnessMatrixAsVector[index] += materailMatrixAsVectorDv[gaussPoint][9] * (bMatrix[shapeFunctionI][gaussPoint][0] * bMatrix[shapeFunctionJ][gaussPoint][0] + bMatrix[shapeFunctionI][gaussPoint][2] * bMatrix[shapeFunctionJ][gaussPoint][2]) + materailMatrixAsVectorDv[gaussPoint][2] * bMatrix[shapeFunctionI][gaussPoint][1] * bMatrix[shapeFunctionJ][gaussPoint][1]; } break; case 5: for (gaussPoint = 0; gaussPoint < ngp3; gaussPoint++) { stiffnessMatrixAsVector[index] += materailMatrixAsVectorDv[gaussPoint][14] * bMatrix[shapeFunctionI][gaussPoint][2] * bMatrix[shapeFunctionJ][gaussPoint][1] + materailMatrixAsVectorDv[gaussPoint][4] * bMatrix[shapeFunctionI][gaussPoint][1] * bMatrix[shapeFunctionJ][gaussPoint][2]; } break; case 8: for (gaussPoint = 0; gaussPoint < ngp3; gaussPoint++) { stiffnessMatrixAsVector[index] += materailMatrixAsVectorDv[gaussPoint][20] * (bMatrix[shapeFunctionI][gaussPoint][0] * bMatrix[shapeFunctionJ][gaussPoint][0] + bMatrix[shapeFunctionI][gaussPoint][1] * bMatrix[shapeFunctionJ][gaussPoint][1]) + materailMatrixAsVectorDv[gaussPoint][5] * bMatrix[shapeFunctionI][gaussPoint][2] * bMatrix[shapeFunctionJ][gaussPoint][2]; } break; } } } break; case ORTHOTROPIC: break;   }```
• April 2nd, 2014, 05:19 AM
GregBrannon
Re: some operation in loop.
Too much code posted improperly (hard to read/understand). Please read this topic to learn how to post code correctly and other useful tips for newcomers.

You are repeating many calculations and method calls inside the loop. Simplifying those so that they are mostly done outside the loops may help. Beyond that, it's hard to see what you're doing with the code posted as it is.