230 likes | 383 Views
Code Tuning and Optimizations. When and How to Improve Code Performance?. Ivaylo Bratoev. Telerik Corporation. www.telerik.com. Actual vs Perceived Performance. Example: “Vista's file copy performance is noticeably worse than Windows XP” – false:
E N D
Code Tuning and Optimizations When and How to Improve Code Performance? Ivaylo Bratoev Telerik Corporation www.telerik.com
Actual vs Perceived Performance • Example: “Vista's file copy performance is noticeably worse than Windows XP” – false: • 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. • 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.
Is performance really a priority • Performance improvements can reduce readability and complexity • “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
How to Improve Performance • Program requirements • Software cost vsperformance • System design • performance-oriented architecture with resource goals for individual subsystems, features, and classes. • Class and method design • data types and algorithms
How to Improve Performance • External Interactions • Operating System • External devices – printers, network, internet • Code Compilation / Code Execution • Compiler optimizations • Hardware • very often the cheapest way • Code Tuning
Introduction to Code Tuning • Modifying correct code to make it run more efficiently • Not the most effective/cheapest way to improve performance • 20% of a program’s methods consume 80% of its execution time.
Code Tuning Myths • Reducing the lines of code in a high-level language improves the speed or size of the resulting machine code – false! 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 for i = 1 to 10 a[ i ] = i end for vs
Code Tuning Myths • A fast program is just as important as a correct one – false!
Code Tuning Myths • Certain operations are probably faster or smaller than others – false! • Always measure performance!
Code Tuning Myths • You should optimize as you go – false! • It is hard to identify bottlenecks before a program is completely working • Focus on optimization detracts from other program objectives
When to tune • Use a high-quality design. • Make the program right. • Make it modular and easily modifiable • When it’s complete and correct, check the performance. • Consider compiler optimizations • Measure • Write clean code that’s easy to understand and modify.
Measurement • Measure to find bottlenecks • Measurements need to be precise • Measurements need to be repeatable
Optimize in iterations • Measure improvement after each optimization • If optimization does not improve performance – revert it
Code Tuning Techniques • Stop Testing When You Know the Answer if ( 5 < x ) and ( y < 10 ) then ... if ( 5 < x ) then if ( y < 10 ) then ... negativeInputFound = False; for ( i = 0; i < iCount; i++ ) { if ( input[ i ] < 0 ) { negativeInputFound = True; } } add a break
Code Tuning Techniques • Order Tests by Frequency Select char Case "+", "=" ProcessMathSymbol(char) Case "0" To "9" ProcessDigit(char) Case ",", ".", "!", "?" ProcessPunctuation(char) Case " " ProcessSpace(char) Case "A" To "Z", "a" To "z“ ProcessAlpha(char) Case Else ProcessError(char) End Select Select char Case "A" To "Z", "a" To "z“ ProcessAlpha(char) Case " " ProcessSpace(char) Case ",", ".", "!", "?" ProcessPunctuation(char) Case "0" To "9" ProcessDigit(char) Case "+", "=" ProcessMathSymbol(char) Case Else ProcessError(char) End Select
Code Tuning Techniques • Unswitching loops for ( i = 0; i < count; i++ ) { if ( sumType == SUMTYPE_NET ) { netSum = netSum + amount[ i ]; } else { grossSum = grossSum + amount[ i ]; } } 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 • Minimizing the work inside loops 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 • Initialize at Compile Time const double Log2 = 0.69314718055994529;
Code Tuning Techniques • Use Lazy Evaluation public int getSize() { if(size == null) { size = the_series.size(); } return size; }
Code Tuning Techniques • Use caching double Hypotenuse(double sideA, double sideB) { return Math.sqrt((sideA*sideA) + (sideB*sideB)); }
Code Tuning Techniques • Use caching (continued) 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 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 and Optimizations ? Questions? ? ? ? ? ? ? ? ? ? http://academy.telerik.com