Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "When and How to Improve Code Performance? Ivaylo Bratoev Telerik Corporation www.telerik.com."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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 ]; }}

17 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;}

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

19 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 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));}

21 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;}

22 Questions? http://academy.telerik.com


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

Similar presentations


Ads by Google