Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: some operation in loop.

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    israel
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default some operation in loop.

    hi friends :

    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 ?

    	// 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;
     
    		}
    Last edited by yosilev; April 2nd, 2014 at 06:30 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,103
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default 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.

Similar Threads

  1. Operation on a txt file..
    By Davidifier in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2012, 01:30 PM
  2. Problems with if else if statement operation
    By Farmer in forum Loops & Control Statements
    Replies: 4
    Last Post: May 29th, 2012, 07:43 AM
  3. Java File Operation
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: March 28th, 2011, 08:54 AM
  4. Byte Operation
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: March 28th, 2011, 12:19 AM
  5. Operation ==
    By meytalg in forum Java Theory & Questions
    Replies: 6
    Last Post: January 4th, 2010, 07:43 PM