SlideShare a Scribd company logo
Code Tuning Techniques Most examples from Code Complete 2
Tuning Code Can be at several “levels” of code Routine to system 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: 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: 1 1 B A 1 C 2 2 3 2 0
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; } 1 1 B A 1 C 2 2 3 2 0
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] 1 1 B A 1 C 2 2 3 2 0
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
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 = 0.69314718; 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
Other Tuning: Buffer I/O Buffer input and output Allows more data to be processed at once Usually 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

More Related Content

What's hot (20)

PPTX
CLR AND LALR PARSER
Akila Krishnamoorthy
 
DOC
CS8391 Data Structures Part B Questions Anna University
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
List comprehensions
Jordi Gómez
 
PPT
Introduction to C++
Bharat Kalia
 
PPT
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
PPT
Strings
Nilesh Dalvi
 
PPTX
Generating code from dags
indhu mathi
 
PPTX
Function C programming
Appili Vamsi Krishna
 
PPTX
Python strings presentation
VedaGayathri1
 
DOCX
Recursion in C++
Maliha Mehr
 
PPSX
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
PPTX
data types in C programming
Harshita Yadav
 
PPTX
If else statement in c++
Bishal Sharma
 
PPTX
PUSH DOWN AUTOMATA VS TURING MACHINE
Abhishek Shivhare
 
PPTX
Syntax Analysis in Compiler Design
MAHASREEM
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
C if else
Ritwik Das
 
PDF
Python Flow Control
Mohammed Sikander
 
CLR AND LALR PARSER
Akila Krishnamoorthy
 
CS8391 Data Structures Part B Questions Anna University
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
List comprehensions
Jordi Gómez
 
Introduction to C++
Bharat Kalia
 
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Strings
Nilesh Dalvi
 
Generating code from dags
indhu mathi
 
Function C programming
Appili Vamsi Krishna
 
Python strings presentation
VedaGayathri1
 
Recursion in C++
Maliha Mehr
 
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
data types in C programming
Harshita Yadav
 
If else statement in c++
Bishal Sharma
 
PUSH DOWN AUTOMATA VS TURING MACHINE
Abhishek Shivhare
 
Syntax Analysis in Compiler Design
MAHASREEM
 
C if else
Ritwik Das
 
Python Flow Control
Mohammed Sikander
 

Viewers also liked (20)

PDF
Code tuning strategies
Asha Sari
 
PDF
Optimization in Programming languages
Ankit Pandey
 
PPTX
Code and memory optimization tricks
DevGAMM Conference
 
PPT
代码大全(内训)
Horky Chen
 
PPTX
程序员实践之路
Horky Chen
 
PDF
高品質軟體的基本動作 101 + 102 for NUU
Su Jan
 
PDF
Design in construction
Asha Sari
 
PDF
Integration
Asha Sari
 
PDF
Defencive programming
Asha Sari
 
PPT
A Guideline to Test Your Own Code - Developer Testing
Folio3 Software
 
PDF
MOST_OpenFoundry_version control system_Git
Su Jan
 
PDF
程序员发展漫谈
Horky Chen
 
PPTX
Java scriptcore brief introduction
Horky Chen
 
PPTX
Variables
Maha Saad
 
PDF
Coding Style
Hung-Wei Liu
 
PDF
Design in construction
Asha Sari
 
PDF
高品質軟體的基本動作 101 for NTHU
Su Jan
 
PPTX
Introduction to code optimization by dipankar
Dipankar Nalui
 
PDF
The pseudocode
Asha Sari
 
Code tuning strategies
Asha Sari
 
Optimization in Programming languages
Ankit Pandey
 
Code and memory optimization tricks
DevGAMM Conference
 
代码大全(内训)
Horky Chen
 
程序员实践之路
Horky Chen
 
高品質軟體的基本動作 101 + 102 for NUU
Su Jan
 
Design in construction
Asha Sari
 
Integration
Asha Sari
 
Defencive programming
Asha Sari
 
A Guideline to Test Your Own Code - Developer Testing
Folio3 Software
 
MOST_OpenFoundry_version control system_Git
Su Jan
 
程序员发展漫谈
Horky Chen
 
Java scriptcore brief introduction
Horky Chen
 
Variables
Maha Saad
 
Coding Style
Hung-Wei Liu
 
Design in construction
Asha Sari
 
高品質軟體的基本動作 101 for NTHU
Su Jan
 
Introduction to code optimization by dipankar
Dipankar Nalui
 
The pseudocode
Asha Sari
 
Ad

Similar to Code Tuning (20)

PDF
Tiling matrix-matrix multiply, code tuning
mukhi265
 
PPT
Code Tuning
guest4df97e3d
 
PDF
Auto Tuning
Hemanth Kumar Mantri
 
PPTX
Compiler optimization techniques
Hardik Devani
 
PPTX
CODE TUNINGtertertertrtryryryryrtytrytrtry
kapib57390
 
PPTX
VCE Unit 01 (2).pptx
skilljiolms
 
PPT
dynamic programming Rod cutting class
giridaroori
 
PPT
Introduction to Algorithms
Venkatesh Iyer
 
PPT
Code Optimization
guest9f8315
 
PPT
code optimization
guest9f8315
 
PDF
Embedded C - Optimization techniques
Emertxe Information Technologies Pvt Ltd
 
PDF
The Inner Secrets of Compilers
IT MegaMeet
 
PDF
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Marina Kolpakova
 
PPTX
#GDC15 Code Clinic
Mike Acton
 
PPT
Memory Optimization
Wei Lin
 
PPT
Memory Optimization
guest3eed30
 
PPTX
Dynamic programming in Design Analysis and Algorithms
NikunjGoyal20
 
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Tiling matrix-matrix multiply, code tuning
mukhi265
 
Code Tuning
guest4df97e3d
 
Compiler optimization techniques
Hardik Devani
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
kapib57390
 
VCE Unit 01 (2).pptx
skilljiolms
 
dynamic programming Rod cutting class
giridaroori
 
Introduction to Algorithms
Venkatesh Iyer
 
Code Optimization
guest9f8315
 
code optimization
guest9f8315
 
Embedded C - Optimization techniques
Emertxe Information Technologies Pvt Ltd
 
The Inner Secrets of Compilers
IT MegaMeet
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Marina Kolpakova
 
#GDC15 Code Clinic
Mike Acton
 
Memory Optimization
Wei Lin
 
Memory Optimization
guest3eed30
 
Dynamic programming in Design Analysis and Algorithms
NikunjGoyal20
 
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Ad

Recently uploaded (20)

PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 

Code Tuning

  • 1. Code Tuning Techniques Most examples from Code Complete 2
  • 2. Tuning Code Can be at several “levels” of code Routine to system 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!
  • 3. Tuning Code We’ll describe several categories of tuning, and several specific cases Logical Approaches Tuning Loops Transforming Data Tuning Expressions Others
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. Logical Approaches: Use Lookup Tables Table lookups can be much faster than following a logical computation Example: diagram of logical values: 1 1 B A 1 C 2 2 3 2 0
  • 8. 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; } 1 1 B A 1 C 2 2 3 2 0
  • 9. 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] 1 1 B A 1 C 2 2 3 2 0
  • 10. 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
  • 11. 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];
  • 12. 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; }
  • 13. 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;
  • 14. 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; }
  • 15. 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
  • 16. 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
  • 17. 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; }
  • 18. 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…
  • 19. 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;
  • 20. 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; }
  • 21. Transforming Data: Use Supplementary Indexes Sort indices in array rather than elements themselves Tradeoff extra dereference in place of copies
  • 22. 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
  • 23. 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
  • 24. Tuning Expressions: Compile-Time Initialization Known constant passed to function can be replaced by value. log2val = log(val) / log(2); const double LOG2 = 0.69314718; log2val = log(val) / LOG2;
  • 25. 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)
  • 26. Tuning Expressions: Use Correct Types Avoid unnecessary type conversions Use floating-point constants for floats, integer constants for ints
  • 27. 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
  • 28. 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;
  • 29. 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
  • 30. 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
  • 31. Other Tuning: Buffer I/O Buffer input and output Allows more data to be processed at once Usually overhead in sending output, getting input
  • 32. 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
  • 33. 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.
  • 34. 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