Professional Documents
Culture Documents
Profile and Optimize
Profile and Optimize
Introduction
O(n) notation is one thing Performance of your coded algorithm is another thing!!! This demo will use Homework #3 ICBaseCycle problem
-Xss<size> set java thread stack size Note: good use in recursive problems
Summary
v1 original code v2 inner loop re-write / algorithm design v3 minimize allocations v4 improved version (repeat (1-4) if necessary) v5 final version
// version 1 private boolean sumElements(int[] permutation){ //generate sumation of terms 1+2+3+...+p int walk, sum; int index = 0; int[] partSums = new int[p * (p-1) + 1];
// i = length of the elements of sum for(int i=1; i<p; i++){ //j = starting index for(int j=0; j<p; j++){ sum = 0; walk = j; //k = loop for summation for(int k=0; k<i; k++){ if(walk == p){ walk = 0; } sum += permutation[walk++]; } partSums[index++] = sum; } }// (cont next slide)
// version 5 private boolean sumElements(int[] permutation, boolean[] partIndexes){ //generate sumation of terms 1+2+3+...+p int value, walk; int pMinusOne = p - 1; Arrays.fill(partIndexes, false); for(int i=0; i<p; i++){ walk = i + 1; partIndexes[(value = permutation[i])] = true; for(int j=1; j<pMinusOne; j++, walk++){ partIndexes[(value += permutation[walk])] = true; } } //skip over index 0, not used //skip over index 1 & 2, always true for(int i=3; i<partIndexes.length; i++){ if(partIndexes[i] == false){ return false; } } return true; }
C:\cs146_profiling>java hw3.v1.ICBaseCycle 8 57 Here is a set of valid arrays 1 8 11 3 18 10 2 4 1 12 2 6 3 7 22 4 1 5 15 7 16 2 8 3 1 8 2 3 21 4 12 6 1 6 17 12 2 11 5 3 1 2 10 19 4 7 9 5 # of solutions: 6 Total compute-time: 110.328 seconds
Conclusion
Easy to start learning how to test the performance on our homework problems Later you will be running large scale applications with multi-tier enterprise applications with networking and database, etc. it will be a lot harder to start learning how to optimize then!!!
References
Effective Java - Joshua Bloch Java Platform Performance Steve Wilson Core Java v.2 Cay Horstmann