Profile and optimize your Java code Gabriel Laden CS 146 – Dr. Sin-Min Lee Spring 2004.

Slides:



Advertisements
Similar presentations
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Advertisements

FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
CS1010 Programming Methodology
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
Complexity (Running Time)
27-Jun-15 Profiling code, Timing Methods. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the.
1 Algorithms and Analysis CS 2308 Foundations of CS II.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
Midterm Test Overview CS221 – 3/23/09. Test Curve Current median is a 71 or C- Median will be adjusted to an 81 or B- All test scores will be +10 on.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Chapter 1 Algorithm Analysis
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
CSC 213 – Large Scale Programming. Why Do We Test?
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 2: Major Concepts of Programming.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester,
CSC 211 Data Structures Lecture 13
CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CS Software Studio Java Lab 1 Meng-Ting Wang PLLAB, Computer Science Department, National Tsing-Hua University.
Introduction to: Programming CS105 Lecture: Yang Mu.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Searching Topics Sequential Search Binary Search.
Written by: Dr. JJ Shepherd
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Lecture #20: Profiling NetBeans Profiler 6.0.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Analysis of Algorithms
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
Describing algorithms in pseudo code
Analysis of Algorithms
CSE 373: Data Structures & Algorithms
Basics of Recursion Programming with Recursion
The structure of programming
Analysis of Algorithms
Presentation transcript:

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

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

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]

-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: log GC status to a file with time stamps Note: see use later in presentation -Xmx 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 set java thread stack size Note: good use in recursive problems

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

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: 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 v1original code v2inner loop re-write / algorithm design v3minimize allocations v4 improved version (…repeat (1-4) if necessary) v5 final version

// version 1 private boolean sumElements(int[] permutation){ //generate sumation of terms 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; } }// (con’t next slide…)

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

// version 5 private boolean sumElements(int[] permutation, boolean[] partIndexes){ //generate sumation of terms 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 # of solutions: 6 Total compute-time: seconds C:\cs146_profiling>java hw3.v5.ICBaseCycle 8 57 Here is a set of valid arrays # of solutions: 6 Total compute-time: 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