Code Tuning Techniques CPSC 315 – Programming Studio Fall 2009 Most examples from Code Complete 2.

Slides:



Advertisements
Similar presentations
Code Tuning Strategies and Techniques
Advertisements

Code Tuning Techniques CPSC 315 – Programming Studio Fall 2008 Most examples from Code Complete 2.
Tori Bowman CSSE 375, Rose-Hulman October 22, Code Tuning (Chapters of Code Complete)
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
DCO1 Performance Measurement and Improvement Lecture 7.
Fall 2011SYSC 5704: Elements of Computer Systems 1 SYSC 5704 Elements of Computer Systems Optimization to take advantage of hardware.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Chapter 10.
Computer Science 1620 Loops.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Program Design and Development
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Computer Science 1620 Programming & Problem Solving.
Recap from last time We saw various different issues related to program analysis and program transformations You were not expected to know all of these.
Portability CPSC 315 – Programming Studio Spring 2008 Material from The Practice of Programming, by Pike and Kernighan.
Code Tuning Techniques CPSC 315 – Programming Studio Spring 2008 Most examples from Code Complete 2.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Code Tuning Strategies and Techniques CS480 – Software Engineering II Azusa Pacific University Dr. Sheldon X. Liang.
Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Fundamentals of Python: First Programs
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
8-1 Embedded Systems Fixed-Point Math and Other Optimizations.
1 Object Oriented Programming Lecture IX Some notes on Java Performance with aspects on execution time and memory consumption.
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.
CPSC 388 – Compiler Design and Construction Optimization.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
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.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Optimization of C Code The C for Speed
Machine Independent Optimizations Topics Code motion Reduction in strength Common subexpression sharing.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
A First Book of ANSI C Fourth Edition
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Code Optimization.
User-Written Functions
Algorithmic complexity: Speed of algorithms
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Loop Structures.
Optimization Code Optimization ©SoftMoore Consulting.
Arrays, For loop While loop Do while loop
Introduction to Object-Oriented Programming with Java--Wu
Algorithmic complexity: Speed of algorithms
Loop Strategies Repetition Playbook.
Code Tuning Techniques
Algorithmic complexity: Speed of algorithms
Presentation transcript:

Code Tuning Techniques CPSC 315 – Programming Studio Fall 2009 Most examples from Code Complete 2

Tuning Code Can be at several “levels” of code Routine level to system level No “do this and improve code” technique Same technique can increase or decrease performance, depending on situation Must measure to see what effect is Remember: Tuning code can make it harder to understand and maintain!

Tuning Code We’ll describe several categories of tuning, and several specific cases Logical Approaches Tuning Loops Transforming Data Tuning Expressions Others

Logical Approaches: Stop Testing Once You Know the Answer Short-Circuit Evaluation if ((a > 1) and (a < 4)) if (a > 1) if (a < 4) Note: Some languages (C++/Java) do this automatically

Logical Approaches: Stop Testing Once You Know the Answer Breaking out of “Test Loops” flag = False; for (i=0; i<10000; i++) { if (a[i] < 0) flag = True; } Several options: Use a break command (or goto!) Change condition to check for Flag Sentinel approach

Logical Approaches: Stop Testing Once You Know the Answer Break Command flag = False; for (i=0; i<10000; i++) { if (a[i] < 0) { flag = True; break(); }

Logical Approaches: Stop Testing Once You Know the Answer Change Condition to Check for Flag flag = False; for (i=0; (i<10000) && !flag; i++) { if (a[i] < 0) { flag = True; }

Logical Approaches: Stop Testing Once You Know the Answer Sentinel Approach flag = False; for (i=0; i<10000; i++) { if (a[i] < 0) { flag = True; i=10000; }

Logical Approaches: Order Tests by Frequency Test the most common case first Especially in switch/case statements Remember, compiler may reorder, or not short- circuit Note: it’s worthwhile to compare performance of logical structures Sometimes case is faster, sometimes if-then Generally a useful approach, but can potentially make tougher-to-read code Organization for performance, not understanding

Logical Approaches: Use Lookup Tables Table lookups can be much faster than following a logical computation Example: diagram of logical values: 11 BA 1 C

Logical Approaches: Use Lookup Tables if ((a && !c) || (a && b && c)) { val = 1; } else if ((b && !a) || (a && c && !b)) { val = 2; } else if (c && !a && !b) { val = 3; } else { val = 0; } 11 BA 1 C

Logical Approaches: Use Lookup Tables static int valtable[2][2][2] = { // !b!c !bc b!c bc 0, 3, 2, 2, // !a 1, 2, 1, 1, // a }; val = valtable[a][b][c] 11 BA 1 C

Logical Approaches: Lazy Evaluation Idea: wait to compute until you’re sure you need the value Often, you never actually use the value! Tradeoff overhead to maintain lazy representations vs. time saved on computing unnecessary stuff

Logical Approaches: Lazy Evaluation Class listofnumbers { private int howmany; private float* list; private float median; float getMedian() { return median; } void addNumber(float num) { //Add number to list //Compute Median }

Logical Approaches: Lazy Evaluation Class listofnumbers { private int howmany; private float* list; private float median; float getMedian() { //Compute Median return median; } void addNumber(float num) { //Add number to list }

Tuning Loops: Unswitching Remove an if statement unrelated to index from inside loop to outside for (i=0; i<n; i++) if (type == 1) sum1 += a[i]; else sum2 += a[i]; if (type == 1) for (i=0; i<n; i++) sum1 += a[i]; else for (i=0; i<n; i++) sum2 += a[i];

Tuning Loops: Jamming Combine two loops for (i=0; i<n; i++) sum[i] = 0.0; for (i=0; i<n; i++) rate[i] = 0.03; for (i=0; i<n; i++) { sum [i] = 0.0; rate[i] = 0.03; }

Tuning Loops: Unrolling Do more work inside loop for fewer iterations Complete unroll: no more loop… Occasionally done by compilers (if recognizable) for (i=0; i<n; i++) { a[i] = i; } for (i=0; i<(n-1); i+=2) { a[i] = i; a[i+1] = i+1; } if (i == n-1) a[n-1] = n-1;

Tuning Loops: Minimizing Interior Work Move repeated computation outside for (i=0; i<n; i++) { balance[i] += purchase->allocator->indiv->borrower; amounttopay[i] = balance[i]*(prime+card)*pcentpay; } newamt = purchase->allocator->indiv->borrower; payrate = (prime+card)*pcentpay; for (i=0; i<n; i++) { balance[i] += newamt; amounttopay[i] = balance[i]*payrate; }

Tuning Loops: Sentinel Values Test value placed after end of array to guarantee termination i=0; found = FALSE; while ((!found) && (i<n)) { if (a[i] == testval) found = TRUE; else i++; } if (found) … //Value found savevalue = a[n]; a[n] = testval; i=0; while (a[i] != testval) i++; if (i<n) … // Value found

Tuning Loops: Busiest Loop on Inside Reduce overhead by calling fewer loops for (i=0; i<100; i++) // 100 for (j=0; j<10; j++) // 1000 dosomething(i,j); 1100 loop iterations for (j=0; j<10; j++) // 10 for (i=0; i<100; i++) // 1000 dosomething(i,j); 1010 loop iterations

Tuning Loops: Strength Reduction Replace multiplication involving loop index by addition for (i=0; i<n; i++) a[i] = i*conversion; sum = 0; // or: a[0] = 0; for (i=0; i<n; i++) { // or: for (i=1; i<n; i++) a[i] = sum; // or: a[i] = sum += conversion; // a[i-1]+conversion; }

Transforming Data: Integers Instead of Floats Integer math tends to be faster than floating point Use ints instead of floats where appropriate Likewise, use floats instead of doubles Need to test on system…

Transforming Data: Fewer Array Dimensions Express as 1D arrays instead of 2D/3D as appropriate Beware assumptions on memory organization for (i=0; i<rows; i++) for (j=0; j<cols; j++) a[i][j] = 0.0; for (i=0; i<rows*cols; i++) a[i] = 0.0;

Transforming Data: Minimize Array Refs Avoid repeated array references Like minimizing interior work for (i=0; i<r; i++) for (j=0; j<c; j++) a[j] = b[j] + c[i]; for (i=0; i<r; i++) { temp = c[i]; for (j=0; j<c; j++) a[j] = b[j] + temp; }

Transforming Data: Use Supplementary Indexes Sort indices in array rather than elements themselves Tradeoff extra dereference in place of copies

Transforming Data: Use Caching Store data instead of (re-)computing e.g. store length of an array (ended by sentinel) once computed e.g. repeated computation in loop Overhead in storing data is offset by More accesses to same computation Expense of initial computation

Tuning Expressions: Algebraic Identities and Strength Reduction Avoid excessive computation sqrt(x) < sqrt(y) equivalent to x < y Combine logical expressions !a || !b equivalent to !(a && b) Use trigonometric/other identities Right/Left shift to multiply/divide by 2 e.g. Efficient polynomial evaluation A*x*x*x + B*x*x + C*x + D = (((A*x)+B)*x)+C)*x+D

Tuning Expressions: Compile-Time Initialization Known constant passed to function can be replaced by value. log2val = log(val) / log(2); const double LOG2 = ; log2val = log(val) / LOG2;

Tuning Expressions: Avoid System Calls Avoid calls that provide more computation than needed e.g. if you need an integer log, don’t compute floating point logarithm Could count # of shifts needed Could program an if-then statement to identify the log (only a few cases)

Tuning Expressions: Use Correct Types Avoid unnecessary type conversions Use floating-point constants for floats, integer constants for ints

Tuning Expressions: Precompute Results Storing data in tables/constants instead of computing at run-time Even large precomputation can be tolerated for good run-time Examples Store table in file Constants in code Caching

Tuning Expressions: Eliminate Common Subexpressions Anything repeated several times can be computed once (“factored” out) instead Compilers pretty good at recognizing, now a = b + (c/d) - e*(c/d) + f*(d/c); t = c/d; a = b + t - e*t + f/t;

Other Tuning: Inlining Routines Avoiding function call overhead by putting function code in place of function call Also called Macros Some languages support directly (C++: inline ) Compilers tend to minimize overhead already, anyway

Other Tuning: Recoding in Low-Level Language Rewrite sections of code in lower-level (and probably much more efficient) language Lower-level language depends on starting level Python -> C++ C++ -> assembler Should only be done at bottlenecks Increase can vary greatly, can easily be worse

Other Tuning: Buffer I/O Buffer input and output Allows more data to be processed at once Usually there is overhead in sending output, getting input

Other Tuning: Handle Special Cases Separately After writing general purpose code, identify hot spots Write special-case code to handle those cases more efficiently Avoid overly complicated code to handle all cases Classify into cases/groups, and separate code for each

Other Tuning: Use Approximate Values Sometimes can get away with approximate values Use simpler computation if it is “close enough” e.g. integer sin/cos, truncate small values to 0.

Other Tuning: Recompute to Save Space Opposite of Caching! If memory access is an issue, try not to store extra data Recompute values to avoid additional memory accesses, even if already stored somewhere

Code Tuning Summary This is a “last” step, and should only be applied when it is needed Always test your changes Often will not improve or even make worse If there is no improvement, go back to earlier version Usually, code readability is more important than performance benefit gained by tuning