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.

Slides:



Advertisements
Similar presentations
Code Tuning Strategies and Techniques
Advertisements

Code Tuning Strategies and Techniques CS524 – Software Engineering Azusa Pacific University Dr. Sheldon X. Liang Mike Rickman.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Important concepts in software engineering The tools to make it easy to apply common sense!
10-Jun-15 Fibonacci Numbers A simple example of program design.
Introduction to Analysis of Algorithms
25-Jun-15 Refactoring III. General philosophy A refactoring is just a way of rearranging code Refactorings are used to solve problems If there’s no problem,
Complexity (Running Time)
Selection Sort, Insertion Sort, Bubble, & Shellsort
Introduction to a Programming Environment
30-Jun-15 Profiling. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the experts say about.
SM3121 Software Technology Mark Green School of Creative Media.
Performance and Code Tuning Overview CPSC 315 – Programming Studio Fall 2009.
Insertion Sort & Shellsort By: Andy Le CS146 – Dr. Sin Min Lee Spring 2004.
1 1 Profiling & Optimization David Geldreich (DREAM)
CPU PROFILING FIND THE BOTTLENECK. WHAT? WHEN? HOW?
Pleasures and Pitfalls of Profiling Primož Gabrijelčič.
JavaScript & Metaperformance Douglas Crockford Yahoo! Inc.
Introduction Optimizing Application Performance with Pinpoint Accuracy What every IT Executive, Administrator & Developer Needs to Know.
Nachos Phase 1 Code -Hints and Comments
CS453 Lecture 3.  A sequential algorithm is evaluated by its runtime (in general, asymptotic runtime as a function of input size).  The asymptotic runtime.
1 ENERGY 211 / CME 211 Lecture 26 November 19, 2008.
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
Profile and optimize your Java code Gabriel Laden CS 146 – Dr. Sin-Min Lee Spring 2004.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
240-Current Research Easily Extensible Systems, Octave, Input Formats, SOA.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CS 3500 L Performance l Code Complete 2 – Chapters 25/26 and Chapter 7 of K&P l Compare today to 44 years ago – The Burroughs B1700 – circa 1974.
Cache Memory By Tom Austin. What is cache memory? A cache is a collection of duplicate data, where the original data is expensive to fetch or compute.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
1 How will execution time grow with SIZE? int array[SIZE]; int sum = 0; for (int i = 0 ; i < ; ++ i) { for (int j = 0 ; j < SIZE ; ++ j) { sum +=
1. 2 Pipelining vs. Parallel processing  In both cases, multiple “things” processed by multiple “functional units” Pipelining: each thing is broken into.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
Real-Time systems By Dr. Amin Danial Asham.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
Maths & Technologies for Games Optimisation for Games 1 CO3303 Week 4.
Bitwise Sort By Matt Hannon. What is Bitwise Sort It is an algorithm that works with the individual bits of each entry in order to place them in groups.
Am I RAM Or am I ROM?.
CSE 373: Data Structures and Algorithms
Compsci 100, Fall Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance  Reason about alternative algorithms.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1 Performance Issues CIS*2450 Advanced Programming Concepts.
AppMetrics for.NET Serviced Components Improving the performance of the Application, by giving you unique visibility and insight into the transaction paths.
COP 3540 Data Structures with OOP
BASICS OF CODE DESIGN.  Modular  Reusable  Easy to Read  Maintainable  Testable  Easy to Change  Easy to Understand THE GOAL.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Lecture #20: Profiling NetBeans Profiler 6.0.
What is it and why do we need it? Chris Ward CS147 10/16/2008.
1 How will execution time grow with SIZE? int array[SIZE]; int sum = 0; for (int i = 0 ; i < ; ++ i) { for (int j = 0 ; j < SIZE ; ++ j) { sum +=
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
The sharing of ideas allows us to stand on one another’s shoulders instead of on one another’s feet Jim Warren.
July 10, 2016ISA's, Compilers, and Assembly1 CS232 roadmap In the first 3 quarters of the class, we have covered 1.Understanding the relationship between.
. A little bit about optimization, 2d array, 1d array used as 2d, register and volatile.
Algorithm Complexity is concerned about how fast or slow particular algorithm performs.
10.3 Details of Recursion.
How will execution time grow with SIZE?
Chapter 9 – Real Memory Organization and Management
Which slows more? Java or C++?
Introduction to Computer Systems
Objective of This Course
Refactoring III 27-Nov-18.
Refactoring III 25-Dec-18.
Performance Measurement
Performance and Code Tuning Overview
Presentation transcript:

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 experts say about optimization: Sir Tony Hoare: “Premature optimization is the root of all evil.” Donald Knuth: “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” W. A. Wulf: “More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity.” Michael A. Jackson: “The First and Second Rules of Program Optimization: 1.Don’t do it. 2.(For experts only!) Don’t do it yet.” Richard Carlson: “Don’t sweat the small stuff.”

When should you optimize? Optimize when (and only when): A program you have written is taking too long to run A method you have written will be called billions of times in a highly graphics-intensive application Your code is going into the kernel of an operating system One of the major principles of the XP (Extreme Programming) approach is: “Do the simplest thing that could possibly work.” In other words, don’t use a faster method in preference to a simpler one If it turns out to be too slow, you can fix it later However: If efficiency is completely ignored in a large project, it may be very difficult to fix it later Individual methods can be improved, but if the overall design of the project is inefficient, redesign can be prohibitively expensive

What should you optimize? From Wikipedia: In engineering, bottleneck is a phenomenon where the performance or capacity of an entire system is limited by a single component The 90/10 rule is: 90% of the runtime is taken up by 10% of the code That 10% is the bottleneck This is where significant improvements can be made How do you find the bottleneck? Intuition is surprisingly inaccurate A profiler is a program that measures which parts of your program are taking the most time (or using the most memory) Use a profiler before you waste your time optimizing the wrong part of the program!

What shouldn’t you optimize? Don’t sweat the small stuff Virtually any modern compiler is better at optimization than you are Consider: for (int i = 0; i limit) array[i] = limit; } Would this be faster if you took the assignment to limit out of the loop? No! The compiler will do that anyway In fact, there is a fair chance that doing so would slow your program down!

How should you optimize? The best way to speed up a bottleneck is to avoid doing it altogether! Example: In one case study I read about, one program added debugging information to a dataset; the dataset was sent to a second program, which stripped off the debugging information before using the dataset Eliminating the debugging information doubled the speed of the program The second best way is to find a faster algorithm This what a course in “Analysis of Algorithms” is all about Always remember, though, that in “real life” you should use a profiler to find out what to optimize The fastest sorting algorithm in the world won’t help you very much if you’re sorting a 10-element array

Timing without a profiler Here’s an easy way to time a section of code: long t = System.currentTimeMillis(); // code to be timed goes here t = System.currentTimeMillis() - t; System.out.println("Code took " + t + " milliseconds"); This approach often fails. Why? Code took 0 milliseconds On modern computers, many things you might want to time take less than one millisecond Solution: Put the code to be timed in a loop, and run it a thousand times, or a hundred thousand times, or even more This helps with the “0 milliseconds” problem By itself, this is a bad idea for timing a sorting algorithm (Why?)

Improving timing accuracy To improve accuracy: Run the code many times, and take the average Close as many other applications as possible--in fact, run from the command line, rather than Eclipse or NetBeans Using currentTimeMillis measures the total elapsed time, by all processes, not just the code you are trying to time Call the code to be timed at least once before you begin timing Many Java compilers don’t compile code until the first time you use it--this is called JIT, or “Just In Time,” compiling Call System.gc() before starting the timing This is a request for Java to run the garbage collector now, so that (hopefully) it will not run during your timing trials This call is legal in all Java implementations, but they are free to ignore it If the code being timed creates new objects, then the time for that will be (and should be) included in the timing results, but may introduce too many garbage collections

Problems with profiling Profiling is not as useful for distributed code For example, an application that runs on a server, accesses a database, makes remote calls, does input/output, etc., is too distributed for profiling to be much help Environment “noise” can be a problem Profiling is more meaningful in the absence of “alien” code, network access, input/output, etc. Try to isolate the code to be profiled from these factors Many Java compilers interpret the code the first time through, rather than compile it Run the code being tested multiple times, rather than just once Longer runs are better; ideally, your profiling run should take tens of seconds, or a few minutes

The End