When and How to Improve Code Performance? Ivaylo Bratoev Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Bellwork If you roll a die, what is the probability that you roll a 2 or an odd number? P(2 or odd) 2. Is this an example of mutually exclusive, overlapping,
Chapter 5: Control Structures II (Repetition)
Analysis of Computer Algorithms
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
ZMQS ZMQS
Telecommunications & Networking
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
Code Tuning Strategies and Techniques
Debugging operating systems with time-traveling virtual machines Sam King George Dunlap Peter Chen CoVirt Project, University of Michigan.
Chapter 18 Methodology – Monitoring and Tuning the Operational System Transparencies © Pearson Education Limited 1995, 2005.
Tori Bowman CSSE 375, Rose-Hulman October 22, Code Tuning (Chapters of Code Complete)
Database Performance Tuning and Query Optimization
Code Tuning Strategies and Techniques CS524 – Software Engineering Azusa Pacific University Dr. Sheldon X. Liang Mike Rickman.
Test on Input, Output, Processing, & Storage Devices
SystemCheck for TestNav. Agenda 2 SystemCheck for TestNav – Overview – iPads and Chromebooks – Accessing SystemCheck SystemCheck – Check Your System Proctor.
Parallel List Ranking Advanced Algorithms & Data Structures Lecture Theme 17 Prof. Dr. Th. Ottmann Summer Semester 2006.
ABC Technology Project
Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1.
Spring 2014 RMS/EOC Proctor Caching Training. Agenda 2 Proctor caching overview Downloading & installing Cache test content.
© S Haughton more than 3?
Progam.-(6)* Write a program to Display series of Leaner, Even and odd using by LOOP command and Direct Offset address. Design by : sir Masood.
Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Corporation
Twenty Questions Subject: Twenty Questions
Squares and Square Root WALK. Solve each problem REVIEW:
Database System Concepts and Architecture
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Introduction to Computer Administration Introduction.
This, that, these, those Number your paper from 1-10.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Processes Management.
Event 4: Mental Math 7th/8th grade Math Meet ‘11.
Addition 1’s to 20.
25 seconds left…...
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
We will resume in: 25 Minutes.
1 Unit 1 Kinematics Chapter 1 Day
Introduction to Recursion and Recursive Algorithms
CS 1 Introduction CS 1 Part 11. Hardware 1.Central Processing Unit (CPU) 2.Main Memory 3.Secondary Memory / Storage 4.Input Devices 5.Output Devices CS.
12-Apr-15 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
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.
Performance and Code Tuning Overview CPSC 315 – Programming Studio Fall 2009.
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.
When and How to Improve Code Performance? Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Code Tuning and Optimization When and How to Improve Code Performance Software University Technical Trainers SoftUni Team.
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.
Operating Systems David Goldschmidt, Ph.D. Computer Science The College of Saint Rose CIS 432.
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.
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
Performance and Code Tuning Overview
Presentation transcript:

When and How to Improve Code Performance? Ivaylo Bratoev Telerik Corporation

Example: Vista's file copy performance is noticeably worse than Windows XP – false: Example: Vista's file copy performance is noticeably worse than Windows XP – false: Vista uses algorithm that perform better in most cases. Vista uses algorithm that perform better in most cases. Explorer waits 12 seconds before providing a copy duration estimate, which certainly provides no sense of smooth progress. Explorer waits 12 seconds before providing a copy duration estimate, which certainly provides no sense of smooth progress. The copy dialog is not dismissed until the write- behind thread has committed the data to disk, which means the copy is slowest at the end. The copy dialog is not dismissed until the write- behind thread has committed the data to disk, which means the copy is slowest at the end. 2

Performance improvements can reduce readability and complexity Performance improvements can reduce readability and complexity premature optimization is the root of all evil - Donald Knuth premature optimization is the root of all evil - Donald Knuth More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. - 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. - W.A. Wulf 3

Program requirements Program requirements Software cost vs performance Software cost vs performance System design System design performance-oriented architecture with resource goals for individual subsystems, features, and classes. performance-oriented architecture with resource goals for individual subsystems, features, and classes. Class and method design Class and method design data types and algorithms data types and algorithms 4

External Interactions External Interactions Operating System Operating System External devices – printers, network, internet External devices – printers, network, internet Code Compilation / Code Execution Code Compilation / Code Execution Compiler optimizations Compiler optimizations Hardware Hardware very often the cheapest way very often the cheapest way Code Tuning Code Tuning 5

Modifying correct code to make it run more efficiently Modifying correct code to make it run more efficiently Not the most effective/cheapest way to improve performance Not the most effective/cheapest way to improve performance 20% of a programs methods consume 80% of its execution time. 20% of a programs methods consume 80% of its execution time. 6

Reducing the lines of code in a high-level language improves the speed or size of the resulting machine code – false! Reducing the lines of code in a high-level language improves the speed or size of the resulting machine code – false! 7 for i = 1 to 10 a[ i ] = i a[ i ] = i end for a[ 1 ] = 1 a[ 2 ] = 2 a[ 3 ] = 3 a[ 4 ] = 4 a[ 5 ] = 5 a[ 6 ] = 6 a[ 7 ] = 7 a[ 8 ] = 8 a[ 9 ] = 9 a[ 10 ] = 10 vs

A fast program is just as important as a correct one – false! A fast program is just as important as a correct one – false! 8

Certain operations are probably faster or smaller than others – false! Certain operations are probably faster or smaller than others – false! Always measure performance! Always measure performance! 9

You should optimize as you go – false! You should optimize as you go – false! It is hard to identify bottlenecks before a program is completely working It is hard to identify bottlenecks before a program is completely working Focus on optimization detracts from other program objectives Focus on optimization detracts from other program objectives 10

Use a high-quality design. Use a high-quality design. Make the program right. Make the program right. Make it modular and easily modifiable Make it modular and easily modifiable When its complete and correct, check the performance. When its complete and correct, check the performance. Consider compiler optimizations Consider compiler optimizations Measure Measure Write clean code thats easy to understand and modify. Write clean code thats easy to understand and modify. 11

Measure to find bottlenecks Measure to find bottlenecks Measurements need to be precise Measurements need to be precise Measurements need to be repeatable Measurements need to be repeatable 12

Measure improvement after each optimization Measure improvement after each optimization If optimization does not improve performance – revert it If optimization does not improve performance – revert it 13

Stop Testing When You Know the Answer Stop Testing When You Know the Answer 14 if ( 5 < x ) and ( y < 10 ) then... if ( 5 < x ) then if ( y < 10 ) then... if ( y < 10 ) then... negativeInputFound = False; for ( i = 0; i < iCount; i++ ) { if ( input[ i ] < 0 ) { if ( input[ i ] < 0 ) { negativeInputFound = True; negativeInputFound = True; }} add a break

Order Tests by Frequency Order Tests by Frequency 15 Select char Case "+", "=" Case "+", "=" ProcessMathSymbol(char) ProcessMathSymbol(char) Case "0" To "9" Case "0" To "9" ProcessDigit(char) ProcessDigit(char) Case ",", ".", "!", "?" Case ",", ".", "!", "?" ProcessPunctuation(char) ProcessPunctuation(char) Case " " Case " " ProcessSpace(char) ProcessSpace(char) Case "A" To "Z", "a" To "z Case "A" To "Z", "a" To "z ProcessAlpha(char) ProcessAlpha(char) Case Else Case Else ProcessError(char) ProcessError(char) End Select Select char Case "A" To "Z", "a" To "z Case "A" To "Z", "a" To "z ProcessAlpha(char) ProcessAlpha(char) Case " " Case " " ProcessSpace(char) ProcessSpace(char) Case ",", ".", "!", "?" Case ",", ".", "!", "?" ProcessPunctuation(char) ProcessPunctuation(char) Case "0" To "9" Case "0" To "9" ProcessDigit(char) ProcessDigit(char) Case "+", "=" Case "+", "=" ProcessMathSymbol(char) ProcessMathSymbol(char) Case Else Case Else ProcessError(char) ProcessError(char) End Select

Unswitching loops Unswitching loops 16 for ( i = 0; i < count; i++ ) { if ( sumType == SUMTYPE_NET ) { if ( sumType == SUMTYPE_NET ) { netSum = netSum + amount[ i ]; netSum = netSum + amount[ i ]; } else { grossSum = grossSum + amount[ i ]; } else { grossSum = grossSum + amount[ i ]; }} if ( sumType == SUMTYPE_NET ) { for ( i = 0; i < count; i++ ) { for ( i = 0; i < count; i++ ) { netSum = netSum + amount[ i ]; } netSum = netSum + amount[ i ]; }} else { for ( i = 0; i < count; i++ ) { for ( i = 0; i < count; i++ ) { grossSum = grossSum + amount[ i ]; } grossSum = grossSum + amount[ i ]; }}

Minimizing the work inside loops Minimizing the work inside loops 17 for (i = 0; i < rateCount; i++) { netRate[i] = baseRate[i] * rates->discounts->factors->net; netRate[i] = baseRate[i] * rates->discounts->factors->net;} quantityDiscount = rates->discounts->factors->net; for (i = 0; i < rateCount; i++) { netRate[i] = baseRate[i] * quantityDiscount; netRate[i] = baseRate[i] * quantityDiscount;}

Initialize at Compile Time Initialize at Compile Time 18 const double Log2 = ;

Use Lazy Evaluation Use Lazy Evaluation 19 public int getSize() { if(size == null) { if(size == null) { size = the_series.size(); size = the_series.size(); } return size; return size;}

20 Use caching Use caching double Hypotenuse(double sideA, double sideB) { return Math.sqrt((sideA*sideA) + (sideB*sideB)); return Math.sqrt((sideA*sideA) + (sideB*sideB));}

Use caching (continued) Use caching (continued) 21 private double cachedHypotenuse = 0; private double cachedSideA = 0; private double cachedSideB = 0; public double Hypotenuse(double sideA, double sideB) { // check to see if the triangle is already in the cache // check to see if the triangle is already in the cache if ((sideA == cachedSideA) && (sideB == cachedSideB)) { if ((sideA == cachedSideA) && (sideB == cachedSideB)) { return cachedHypotenuse; return cachedHypotenuse; } // compute new hypotenuse and cache it // compute new hypotenuse and cache it cachedHypotenuse = Math.sqrt((sideA*sideA) + (sideB*sideB)); cachedHypotenuse = Math.sqrt((sideA*sideA) + (sideB*sideB)); cachedSideA = sideA; cachedSideA = sideA; cachedSideB = sideB; cachedSideB = sideB; return cachedHypotenuse; return cachedHypotenuse;}

Questions?