You are on page 1of 17

Profile and optimize your Java code

Gabriel Laden CS 146 – Dr. Sin-Min Lee Spring 2004

Introduction • O(n) notation is one thing • Performance of your coded algorithm is another thing!!! • This demo will use Homework #3 “ICBaseCycle” problem .

.currentTimeMillis().Where to start testing? • To find run-time between two sequential lines in your code insert the following: System.

The 80/20 rule • Your code spends 80% of its time in 20% of the code. or so they say… • How to find this hotspot in your code without just guessing : java –Xprof class [args] .

java –Xprof class [args] • -Xprof is a built-in profiler for your code • It tells you what % time is spent in running and other Java administrative tasks • Caution: eliminate your user I/O and other things like that which are not constant and will throw off your testing results .

Other interesting java –X options • -Xloggc:<file> log GC status to a file with time stamps Note: see use later in presentation • -Xmx<size> set maximum Java heap size Note: 64M is default. good use is for problems of moderately larger size. (go buy more memory at Fry’s) • -Xss<size> set java thread stack size Note: good use in recursive problems .

i.. javap prints out the package.javap . If no options are used.The Java Class File Disassembler javap [options] class • The javap command disassembles a class file.e. protected. Its output depends on the options used. and public fields and methods of the classes passed to it. the instructions that comprise the Java bytecodes . • -c option prints out disassembled code.

Most Important Step!!! • Find your inner-most loop that is in method with largest run-time % • Re-write this loop to be most efficient • This is where O(n) notation theory can be applied to your algorithm .

Java handles your garbage • So don’t forget to analyze this!!! • java -Xloggc:<file> class • The cost of gc also implies the cost of the allocation • The number of lines written to filegc will be evidence of the garbage collector activity • Look in your code for unneccessary allocations • Re-order processing to keep heap size to a minimum .

Try a few different things • • • • • Is your sort algorithm really the best one? Is your data structure really the best one? Save your working program to a backup!!! Compare run-times with different sorts Do the least # operations to get your output .

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 .

int[] partSums = new int[p * (p-1) + 1]. i<p. k++){ if(walk == p){ walk = 0. //k = loop for summation for(int k=0. sum. k<i..+p int walk. j<p.// version 1 private boolean sumElements(int[] permutation){ //generate sumation of terms 1+2+3+.. } partSums[index++] = sum. } sum += permutation[walk++]. // i = length of the elements of sum for(int i=1. } }// (con’t next slide…) . int index = 0. walk = j. i++){ //j = starting index for(int j=0. j++){ sum = 0.

} .java in j2sdk demo directory QSort.// version 1 (con’t) //add last element sum of all elements partSums[index] = n. //step through array to verify success int consecutive = 1. for(int i=1.length-1). } else if(partSums[i] > consecutive + 1){ return false. 0.length. i<partSums. i++){ if(partSums[i] == consecutive + 1){ consecutive++. //qsort rewritten from QSortAlgorithm. partSums. } } return true.qsort(partSums.

boolean[] partIndexes){ //generate sumation of terms 1+2+3+. i<partIndexes. i++){ if(partIndexes[i] == false){ return false. always true for(int i=3. partIndexes[(value = permutation[i])] = true.. j<pMinusOne. i++){ walk = i + 1. for(int j=1. Arrays. for(int i=0. j++. walk++){ partIndexes[(value += permutation[walk])] = true.+p int value.1. false). } .fill(partIndexes.. not used //skip over index 1 & 2. walk. i<p. } } return true.// version 5 private boolean sumElements(int[] permutation. int pMinusOne = p .length. } } //skip over index 0.

v5.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: 10.328 seconds C:\cs146_profiling>java hw3.078 seconds .C:\cs146_profiling>java hw3.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.

it will be a lot harder to start learning how to optimize then!!! .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.

References • Effective Java .Joshua Bloch • Java Platform Performance – Steve Wilson • Core Java v.2 – Cay Horstmann .