Code Tuning Strategies and Techniques CS524 – Software Engineering Azusa Pacific University Dr. Sheldon X. Liang Mike Rickman.

Slides:



Advertisements
Similar presentations
Analysis of Computer Algorithms
Advertisements

Chapter 4 Computation Bjarne Stroustrup
Code Tuning Strategies and Techniques
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)
When and How to Improve Code Performance? Ivaylo Bratoev Telerik Corporation
1 Optimizing compilers Managing Cache Bercovici Sivan.
Chair of Software Engineering From Program slicing to Abstract Interpretation Dr. Manuel Oriol.
Programming Logic and Design Fourth Edition, Introductory
Online Performance Auditing Using Hot Optimizations Without Getting Burned Jeremy Lau (UCSD, IBM) Matthew Arnold (IBM) Michael Hind (IBM) Brad Calder (UCSD)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
UNIX Chapter 01 Overview of Operating Systems Mr. Mohammad A. Smirat.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Copyright © 1998 Wanda Kunkle Computer Organization 1 Chapter 2.1 Introduction.
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.
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.
Programming Fundamentals (750113) Ch1. Problem Solving
Performance and Code Tuning Overview CPSC 315 – Programming Studio Fall 2009.
Oakkar Fall The Need for Decision Engine Automate business processes Implement complex business decision logic Separation of rules and process Business.
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.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Dr. Ken Hoganson, © August 2014 Programming in R STAT8030 Programming in R COURSE NOTES 1: Hoganson Programming Languages.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Programming Logic and Design Fourth Edition, Introductory
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Weka Project assignment 3
Lecture 1: Performance EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2013, Dr. Rozier.
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.
Software Construction and Evolution - CSSE 375 Code Tuning Shawn and Steve Left – Even tuning an ancient instrument like a violin involves multiple steps.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
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.
Construction Planning and Prerequisite
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
CS 127 Introduction to Computer Science. What is a computer?  “A machine that stores and manipulates information under the control of a changeable program”
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
Decision Making and Branching (cont.)
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Custom Computing Machines for the Set Covering Problem Paper Written By: Christian Plessl and Marco Platzner Swiss Federal Institute of Technology, 2002.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
Introduction to Computer Programming using Fortran 77.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Software Engineering Algorithms, Compilers, & Lifecycle.
Introduction to Algorithms
Advanced Computer Systems
Code Optimization.
Introduction To Repetition The for loop
Creativity in Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
` Structured Programming & Flowchart
Problem Solving Skill Area 305.1
Searching, Sorting, and Asymptotic Complexity
Algorithms and Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Introduction to Algorithms
Flowcharts and Pseudo Code
Programming Fundamentals (750113) Ch1. Problem Solving
Data Structures Introduction
Performance and Code Tuning Overview
Presentation transcript:

Code Tuning Strategies and Techniques CS524 – Software Engineering Azusa Pacific University Dr. Sheldon X. Liang Mike Rickman

Code Tuning Strategies and Techniques Overview Performance and Code Tuning Introduction to Code Tuning Common Sources of Inefficiency Measurement Iteration Code-Tuning Techniques Checklist & Summary

Performance Overview More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason including blind stupidityW. A. Wulf Users more interested in program throughput than raw performance. Clean, usable user interface Robust software

Performance and Code Tuning When efficiency is chosen as priority, consider the following: Program requirements Are the requirements stating performance realistic? Analyze performance level versus cost. Program design Modular system design provides easier tuning Set goals for individual components Use of sub-systems can improve performance Class and routine design Choice of data types Choice of algorithms

Performance and Code Tuning cont. Operating-system interactions Inefficient operating system routines Compiler generated system calls Code compilation Good compilers optimize code speed Hardware New hardware may be cheaper than optimizing code Code tuning(Lastly) Practice of modifying correct code in ways that make it run more efficiently

Introduction to Code Tuning Code Tunings Appeal Its fun, its cool, and it makes me look smart Pareto Principle (80/20 Rule) Know what to tune Go after the big problems and ignore the little ones Measure Twice – Cut once Measure performance to know what needs to be improved

Introduction to Code Tuning More lines of code = less efficient - FALSE You should optimize as you go – FALSE Make it work correctly first What do you want to tune for? Code Size versus Speed Use a worker thread The appearance of performance

Introduction to Code Tuning When to tune Jackson's Rules of Optimization: Rule 1. Don't do it. Rule 2 (for experts only). Don't do it yetthat is, not until you have a perfectly clear and unoptimized solution. M. A. Jackson Use compiler optimization Write clear code Let the compiler to the optimizing

Common Sources of Inefficiency Input / Output Operations In memory operation much faster than disk access Organize and minimize I/O operations Paging Operation that causes the operating system to swap pages of memory is much slower than an operation that works on only one page of memory for ( column = 0; column < MAX_COLUMNS; column++ ) { for ( row = 0; row < MAX_ROWS; row++ ) { table[ row ][ column ] = BlankTableElement(); }} for ( row = 0; row < MAX_ROWS; row++ ) { for ( column = 0; column < MAX_COLUMNS; column++ ) { table[ row ][ column ] = BlankTableElement(); }}

Common Sources of Inefficiency System calls Calls to system routines are often expensive. Context Switch Possible Solutions Write your own services. Avoid going to the system. Work with the system vendor to make the call faster. Interpreted languages Interpreted languages exact significant performance penalties If performance matters, dont use them

Measurement Measure your code to find the hot spots You dont know if or how much your improving if you dont measure Measurement needs to be precise QueryPerformanceCounter – Windows Only measure the code your tuning

Iteration Multiple techniques may be necessary Cumulative effect of tuning

Code-Tuning Techniques Logic Stop Testing When You Know the Answer negativeInputFound = false; for ( i = 0; i < count; i++ ) { if ( input[ i ] < 0 ) { negativeInputFound = true; break; } Consider order of evaluation if ( 5 < x ) and ( x < 10 ) then...

Code-Tuning Techniques Order Tests by Frequency Arrange tests so that the one that's fastest and most likely to be true is performed first Select inputCharacter Case "A" To "Z", "a" To "z ProcessAlpha( inputCharacter ) Case " ProcessSpace( inputCharacter ) Case ",", ".", ":", ";", "!", "? ProcessPunctuation( inputCharacter ) Case "0" To "9 ProcessDigit( inputCharacter ) Case "+", "=" ProcessMathSymbol( inputCharacter ) Case Else ProcessError( inputCharacter ) End Select

Code-Tuning Techniques Compare Performance of Similar Logic Structures Test from case statement versus if-then-else logic. Languagecaseif-then- else Time Savings Performance Ratio C# %1:1 Java %6:1 Visual Basic %1:4 This example illustrates the difficulty of performing any sort of "rule of thumb" to code tuning There is simply no reliable substitute for measuring results.

Code-Tuning Techniques Substitute Table Lookups for Complicated Expressions A table lookup might be quicker than traversing a complicated chain of logic. The point of a complicated chain is usually to categorize something and then to take an action based on its category. Suppose you want to assign a category number to something based on which of three groups Groups A, B, and Cit falls into:

Code-Tuning Techniques Substitute Table Lookups for Complicated Expressions if ( ( a && !c ) || ( a && b && c ) ) { category = 1; } else if ( ( b && !a ) || ( a && c && !b ) ) { category = 2; }else if ( c && !a && !b ) { category = 3; } else { category = 0;}

Code-Tuning Techniques Substitute Table Lookups for Complicated Expressions // define categoryTable static int categoryTable[ 2 ][ 2 ][ 2 ] = { <-- 1 // !b!c !bc b!c bc 0, 3, 2, 2, // !a 1, 2, 1, 1 // a };... category = categoryTable[ a ][ b ][ c ]; Although the definition of the table is hard to read, if it's well documented it won't be any harder to read than the code for the complicated chain of logic was. If the definition changes, the table will be much easier to maintain than the earlier logic would have been. Here are the performance results:

Code-Tuning Techniques Use Lazy Evaluation Program contains a table of 5000 values Generates the whole table at startup time, and then uses it as the program executes. If the program uses only a small percentage of the entries in the table, it might make more sense to compute them as they're needed rather than all at once.

Code-Tuning Techniques Loops Unswitching EX1 tests sumType every iteration. for ( i = 0; i < count; i++ ) { if ( sumType == SUMTYPE_NET ) { netSum = netSum + amount[ i ]; } else { grossSum = grossSum + amount[ i ]; } EX2 only tests sumType once. if ( sumType == SUMTYPE_NET ) { for ( i = 0; i < count; i++ ) { netSum = netSum + amount[ i ]; } else { for ( i = 0; i < count; i++ ) { grossSum = grossSum + amount[ i ]; }

Code-Tuning Techniques Loops Jamming Jamming is the result of combining two loops that operate on the same set of elements For i = 0 to employeeCount – 1 employeeName( i ) = Next For i = 0 to employeeCount – 1 employeeEarnings( i ) = 0 next For i = 0 to employeeCount – 1 employeeName( i ) = employeeEarnings( i ) = 0 Next

Code-Tuning Techniques Loops Unrolling The goal of loop unrolling is to reduce the amount of loop housekeeping i = 0; while ( i < count ) { a[ i ] = i; i = i + 1; } i = 0; while ( i < count - 1 ) { a[ i ] = i; a[ i + 1 ] = i + 1; i = i + 2; } // These lines pick up the case that might fall through the cracks if the loop went by twos instead of by ones. if ( i == count ) { a[ count - 1 ] = count - 1; }

Code-Tuning Techniques Loops Minimizing the Work Inside Loops One key to writing effective loops is to minimize the work done inside a loop for ( i = 0; i < rateCount; i++ ) { netRate[ i ] = baseRate[ i ] * rates->discounts->factors->net; } quantityDiscount = rates->discounts->factors->net; for ( i = 0; i < rateCount; i++ ) { netRate[ i ] = baseRate[ i ] * quantityDiscount; }

Code-Tuning Techniques Loops Sentinel Values Use of a sentinel is the search loop that checks both whether it has found the value it's seeking and whether it has run out of values. found = FALSE; i = 0; while ( ( !found ) && ( i < count ) ) { if ( item[ i ] == testValue ) { found = TRUE; } else { i++; } if ( found ) {...

Code-Tuning Techniques Loops Sentinel Values In this example, a sentinel value is a value that you put just past the end of the search range that's guaranteed to terminate the search. // set sentinel value, preserving the original valueinitialValue = item[ count ]; item[ count ] = testValue; i = 0; while ( item[ i ] != testValue ) { i++;} // check if value was found if ( i < count ) {... Remember to allow space for the sentinel value at the end of the array

Code-Tuning Techniques Loops Putting the Busiest Loop on the Inside When you have nested loops, think about which loop you want on the outside and which you want on the inside for ( column = 0; column < 100; column++ ) { for ( row = 0; row < 5; row++ ) { sum = sum + table[ row ][ column ]; } By merely switching the inner and outer loops, you can change the total number of iterations from 600 to 505.

Code-Tuning Techniques Loops Strength Reduction Reducing strength means replacing an expensive operation such as multiplication with a cheaper operation such as addition For i = 0 to saleCount – 1 commission( i ) = (i + 1) * revenue * baseCommission * discount Next incrementalCommission = revenue * baseCommission * discount cumulativeCommission = incrementalCommission For i = 0 to saleCount – 1 commission( i ) = cumulativeCommission cumulativeCommission = cumulativeCommission + incrementalCommission Next

Code-Tuning Techniques Data Transformation Changes in data types can be a powerful aid in reducing program size and improving execution speed Integer addition and multiplication tend to be faster than floating point. Changing a loop index from a floating point to an integer, for example, can save time Dim x As Single For x = 0 to 99 a( x ) = 0 Next

Code-Tuning Techniques Use the Fewest Array Dimensions Possible Conventional wisdom maintains that multiple dimensions on arrays are expensive. If you can structure your data so that it's in a one-dimensional array rather than a two- dimensional or three-dimensional array, you might be able to save some time

Code-Tuning Techniques Minimize Array References Advantageous to minimize array accesses The reference to discount[ discountType ] doesn't change when discountLevel changes in the inner loop for ( discountType = 0; discountType < typeCount; discountType++ ) { for ( discountLevel = 0; discountLevel < levelCount; discountLevel++ ) { rate[ discountLevel ] = rate[ discountLevel ] * discount[ discountType ]; }

Code-Tuning Techniques Minimize Array References Move discount [discountType] out of the inner loop so that you'll have only one array access per execution of the outer loop for ( discountType = 0; discountType < typeCount; discountType++ ) { thisDiscount = discount[ discountType ]; for ( discountLevel = 0; discountLevel < levelCount; discountLevel++ ) { rate[ discountLevel ] = rate[ discountLevel ] * thisDiscount; }

Code-Tuning Techniques Use Supplementary Indexes Using a supplementary index means adding related data that makes accessing a data type more efficient Example In C, strings are terminated by a byte that's set to 0 In Visual Basic string format, a length byte hidden at the beginning of each string indicates how long the string is Which is faster at determining a string length? Use this concept for determining length of an variable- length data

Code-Tuning Techniques Use Caching Caching means saving a few values in such a way that you can retrieve the most commonly used values more easily than the less commonly used values You can cache the results of time-consuming computations too public double Hypotenuse( double sideA, double sideB ) { // check to see if the triangle is already in the cache if ( ( sideA == cachedSideA ) && ( sideB == cachedSideB ) ) { return cachedHypotenuse; } // compute new hypotenuse and cache it cachedHypotenuse = Math.sqrt( ( sideA * sideA ) + ( sideB * sideB ) ); cachedSideA = sideA; cachedSideB = sideB; return cachedHypotenuse; }

Code-Tuning Techniques Expressions Much of the work in a program is done inside mathematical or logical expressions. Complicated expressions tend to be expensive Exploit Algebraic Identities Use algebraic identities to replace costly operations with cheaper ones. The following expressions are logically equivalent: not a and not b not (a or b)

Code-Tuning Techniques Use Strength Reduction Strength reduction means replacing an expensive operation with a cheaper one Replace multiplication with addition. Replace exponentiation with multiplication. Replace longlong integers with longs or ints Replace floating-point numbers with fixed-point numbers or integers. Replace double-precision floating points with single- precision numbers. Replace integer multiplication-by-two and division-by-two with shift operations.

Code-Tuning Techniques Initialize at Compile Time If you're using a named constant or a magic number in a routine call, that's a clue that you could pre-compute the number unsigned int Log2( unsigned int x ) { return (unsigned int) ( log( x ) / log( 2 ) ); } Replace log(2) with a constant

Code-Tuning Techniques Use the Correct Type of Constants Use named constants and literals that are the same type as the variables they're assigned to. A good compiler does the type conversion at compile time But dont take the chance

Code-Tuning Techniques Precompute Results A common low-level design decision is the choice of whether to compute results on the fly or compute them once and look them up as needed. Code Size versus Memory Use Compute a lookup table once when program execution begins, using it every time thereafter Store results in a data file or embed them in a program.

Code-Tuning Techniques Eliminate Common Subexpressions If an expression is repeated several times, assign it to a variable rather than re-computing in several places. payment = loanAmount / (( Math.pow( ( interestRate / 12.0 ), -months ) ) / ( interestRate / 12.0 ) ); interestRate / 12.0 could be a variable

Code-Tuning Techniques Routines Good routine decomposition is best for well tuned code Small, well-defined routines save space Take the place of doing jobs separately in multiple places You can re-factor code in one routine and thus improve every routine that calls it Small routines are relatively easy to rewrite in a low-level language

Code-Tuning Techniques Routines Rewrite Routines Inline Code executes in-place versus calling a routine Less advantage with newer compilers and computers Recoding in a Low-Level Language If you're coding in C++, the low-level language might be assembler. If you're coding in Python, the low-level language might be C.

Code-Tuning Techniques Checklist Improve Both Speed and Size Substitute table lookups for complicated logic. Jam loops. Use integer instead of floating-point variables. Initialize data at compile time. Use constants of the correct type. Precompute results. Eliminate common sub-expressions. Translate key routines to a low-level language.

Code-Tuning Techniques Checklist Improve Speed Only Stop testing when you know the answer. Order tests in case statements and if-then-else chains by frequency. Compare performance of similar logic structures. Use lazy evaluation. Unswitch loops that contain if tests. Unroll loops. Minimize work performed inside loops. Use sentinels in search loops. Put the busiest loop on the inside of nested loops.

Code-Tuning Techniques Checklist Improve Speed Only (cont.) Reduce the strength of operations performed inside loops. Change multiple-dimension arrays to a single dimension. Minimize array references. Augment data types with indexes. Cache frequently used values. Exploit algebraic identities. Reduce strength in logical and mathematical expressions. Be wary of system routines. Rewrite routines inline.

Summary of the Approach to Code Tuning 1.Develop the software by using well-designed code that's easy to understand and modify. 2.If performance is poor, a.Save a working version of the code so that you can get back to the "last known good state." b.Measure the system to find hot spots. c.Determine whether the weak performance comes from inadequate design, data types, or algorithms and whether code tuning is appropriate. If code tuning isn't appropriate, go back to step 1. d.Tune the bottleneck identified in step (c). e.Measure each improvement one at a time. f.If an improvement doesn't improve the code, revert to the code saved in step (a). (Typically, more than half the attempted tunings will produce only a negligible improvement in performance or degrade performance.) 3.Repeat from step 2.