1 Performance Issues CIS*2450 Advanced Programming Concepts.

Slides:



Advertisements
Similar presentations
Code Tuning Strategies and Techniques
Advertisements

Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Fundamentals of Python: From First Programs Through Data Structures
COS 312 DAY 16 Tony Gauvin. Ch 1 -2 Agenda Questions? Next progress report is March 26 Assignment 4 Corrected – 3 A’s, 1 C, 1 D, 1 F and 1 MIA Assignment.
Chapter 11 Analysis of Algorithms. Chapter Scope Efficiency goals The concept of algorithm analysis Big-Oh notation The concept of asymptotic complexity.
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
1 Lecture 6 Performance Measurement and Improvement.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 34: Code Optimization COMP 144 Programming Language Concepts Spring 2002 Felix.
Algorithm Efficiency and Sorting
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.
Performance Improvement
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.
CSc 352 Performance Tuning Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
1 Performance Measurement CSE, POSTECH 2 2 Program Performance Recall that the program performance is the amount of computer memory and time needed to.
1 1 Profiling & Optimization David Geldreich (DREAM)
Chocolate Bar! luqili. Milestone 3 Speed 11% of final mark 7%: path quality and speed –Some cleverness required for full marks –Implement some A* techniques.
Slides Prepared from the CI-Tutor Courses at NCSA By S. Masoud Sadjadi School of Computing and Information Sciences Florida.
Visual C New Optimizations Ayman Shoukry Program Manager Visual C++ Microsoft Corporation.
Compiler Code Optimizations. Introduction Introduction Optimized codeOptimized code Executes faster Executes faster efficient memory usage efficient memory.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
COMP 232 Intro Lecture. Introduction to Course Me – Dr. John Sigle Purpose/goals of the course Purpose/goals Prerequisites - COMP 132 (Java skill & Eclipse)
Performance Optimization Getting your programs to run faster CS 691.
Chapter 25: Code-Tuning Strategies. Chapter 25  Code tuning is one way of improving a program’s performance, You can often find other ways to improve.
Computer Science 313 – Advanced Programming Topics.
Application Profiling Using gprof. What is profiling? Allows you to learn:  where your program is spending its time  what functions called what other.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 12/6/2006 Lecture 24 – Profilers.
Timing Programs and Performance Analysis Tools for Analysing and Optimising advanced Simulations.
Performance Optimization Getting your programs to run faster.
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.
Tom Cargill The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the.
Adv. UNIX: Profile/151 Advanced UNIX v Objectives –introduce profiling based on execution times and line counts Special Topics in Comp.
SDD/DFS Jonas M. Larsen VLT 2 nd Generation Instrumentation Pipelines, 19 Apr Jonas M. Larsen Memory debugging Recipe profiling.
1 Announcements  Homework 4 out today  Dec 7 th is the last day you can turn in Lab 4 and HW4, so plan ahead.
Outline Announcements: –HW III due Friday! –HW II returned soon Software performance Architecture & performance Measuring performance.
Performance Improvement
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
Optimization of C Code The C for Speed
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Copyright 2014 – Noah Mendelsohn Code Tuning Noah Mendelsohn Tufts University Web:
Single Node Optimization Computational Astrophysics.
Searching Topics Sequential Search Binary Search.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Copyright 2014 – Noah Mendelsohn Performance Analysis Tools Noah Mendelsohn Tufts University Web:
Performance* Objective: To learn when and how to optimize the performance of a program. “ The first principle of optimization is don ’ t. ” –Knowing how.
Outline Announcements: –HW II Idue Friday! Validating Model Problem Software performance Measuring performance Improving performance.
Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Optimization. How to Optimize Code Conventional Wisdom: 1.Don't do it 2.(For experts only) Don't do it yet.
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.
Precept 10 : Building and Performance
Code Optimization.
Dependence Analysis Important and difficult
Chapter 11 Analysis of Algorithms
Code Optimization I: Machine Independent Optimizations
Performance Optimization for Embedded Software
Chapter 2 Analysis of Algorithms
Chapter 2 Analysis of Algorithms
Compiler Code Optimizations
CSE 403 Lecture 17 Coding.
Multithreading Why & How.
C. M. Overstreet Old Dominion University Spring 2006
Bubble sort.
Analysis of Algorithms
CIS*2450 Advanced Programming Concepts
Optimization.
C. M. Overstreet Old Dominion University Fall 2005
C. M. Overstreet Old Dominion University Fall 2007
Introduction to Optimization
CSc 352 Performance Tuning
Presentation transcript:

1 Performance Issues CIS*2450 Advanced Programming Concepts

2 References The Practice of Programming by Brian W. Kernighan and Rob Pike, Addison-Wesley, 1999.

3 References Programming Pearls, 2 nd Edition, by Jon Bentley, Addison-Wesley, 2000.

4 Speed! Carefully determine if program speed is critical for your application. Ask yourself the following questions: –When should you try to speed up a program? –How can this be done? –What do you expect to gain?

5 The First Principle of Optimization Don't!

6 Steps to Optimization 1.Start with the simplest, cleanest algorithms and data structures appropriate for the task. 2.Measure performance to see if changes are necessary. 3.Enable compiler options to generate the fastest possible code.

7 Steps to Optimization 4.Assess possible changes to the program and their effect -- choose the ones that have the most effect. 5.Make changes one at a time. 6.Re-assess. Compare to the original version for correctness and performance.

8 Performance Tools

9 Automated Timing Measurements #include int main ( int argc, char *argv[] ) { clock_t before; double elapsed; before = clock(); count = FileInput( f_input, &allstrings ); elapsed = clock() - before; printf("FileInput function used %.3f seconds\n", elapsed/CLOCKS_PER_SEC);

10 Automated Timing Measurements before = clock(); for ( j=0; j < count; j++ ) { i = PatternSearch ( j, allstrings, pattern ); if ( i != -1 ) { printf("Found: line %d (%d)\n",j,i+1); } elapsed = clock() - before; printf("%d calls to the PatternSearch function used %.3f seconds\n", count,elapsed/CLOCKS_PER_SEC);

11 Profilers A profile is a measurement of where a program spends its time. A profiler is an effective tool for finding hot spots in a program –functions –areas of sections of code which are consuming most of the processing time.

12 Profilers In 1971, Donald Knuth wrote, ``…less than 4 per cent of a program generally accounts for more than half of its running time.’’

13 Profilers > gcc -pg slow1.c -o slow1 > slow1 Big pattern > gprof slow1 Flat profile: Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls us/call us/call name PatternSearch mcount FileInput main

14 Strategies for Speed

15 Use a Better Algorithm or Data Structure Most important factor in making a program faster.

16 Hot Spot Code int PatternSearch ( int j, char *all, char *pat) { int i; for ( i=0; i < strlen(all+j*100); i++ ) { if ( strncmp(pat, ((all+j*100)+i), strlen(pat)) == 0 ) { return(i); } return(-1); }

17 Hot Spot after Modification int PatternSearch ( int j, char *all, char *pat) { int i; char firstletter = *(pat+0); for ( i=0; i < strlen(all+j*100); i++ ) { if ( *((all+j*100)+i) == firstletter ) { if ( strncmp(pat, ((all+j*100)+i), strlen(pat)) == 0 ) { return(i); } return(-1); }

18 Hot Spot Timing Differences Before: –FileInput function used seconds –28008 calls to the PatternSearch function used seconds After: –FileInput function used seconds –28008 calls to the PatternSearch function used seconds

19 Enable Compiler Optimization Without -O, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results. Optimizing compilation takes somewhat more time, and a lot more memory for a large function. The compiler tries to reduce code size and execution time.

20 Tune the Code Read the Rules for Code Tuning in Programming Pearls by Bentley. Available as a link off of the course website.

21 Don't Optimize What Doesn't Matter. Remember the first principle of optimization! Determine which time is more important: –Time to delivery –Execution time

22 Tuning

23 The PatternSearch Function int PatternSearch ( int j, char *all, char *pat) { int i; char firstletter = *(pat+0); int n = strlen(all+j*100); int p = strlen(pat); for ( i=0; i < n; i++ ) { if ( *((all+j*100)+i) == firstletter ) { if ( strncmp(pat, ((all+j*100)+i), p) == 0 ) { return(i); } return(-1); }

24 Timing FileInput function used seconds calls to the PatternSearch func used secs FileInput function used seconds calls to the PatternSearch func used secs FileInput function used seconds calls to the PatternSearch func used secs

25 Profiling % cumulative self self total time seconds seconds calls us/call us/call name PatternSearch mcount main PatternSearch main PatternSearch mcount main

26 Tuning for Speed

27 Unroll or Eliminate Loops There is overhead involved in setting up and running a loop. If the body of the loop is not too long and there are not too many iterations, it can be more efficient to eliminate the loop.

28 Unroll or Eliminate Loops The Loop for ( j=0; j < ; j++ ) { for ( i=0; i < 3; i++ ) { a[i] = b[i] + c[i]; } No Loop for ( j=0; j < ; j++ ) { a[0] = b[0] + c[0]; a[1] = b[1] + c[1]; a[2] = b[2] + c[2]; }

29 Unroll or Eliminate Loops Timing > loop repetitions of the inner loop used seconds > loop repetitions of the additions used seconds

30 Unroll or Eliminate Loops Longer Loops –If the loop is longer, you can transform the loop to eliminate some of the looping –“Partial unrolling” for ( j=0; j < ; j++ ) for ( i=0; i < XXX; i++ ) a[i] = b[i] + c[i]; Becomes... for ( j=0; j < ; j++ ) { for ( i=0; i < XXX; i += 3 ) { a[i] = b[i] + c[i]; a[i+1] = b[i+1] + c[i+1]; a[i+2] = b[i+2] + c[i+2]; } where XXX was 9, 99 and 198.

31 Unroll or Eliminate Loops Timing For 9 iterations: –The loop used seconds –The modified loop used seconds For 99 iterations: –The loop used seconds –The modified loop used seconds For 198 iterations: –The loop used seconds –The modified loop used seconds