SlideShare a Scribd company logo
Code-Tuning Strategies

Code Complete
Author : Steven C. McConnell.

Prof. Asha N

1
Code-Tuning Strategies

1.
2.

3.
4.
5.

Performance Overview
Introduction to Code Tuning
Kinds of Fat and Molasses
Measurement
Iteration

Prof. Asha N

2
1. Performance Overview


Code tuning is one way of improving a program’s performance
 Quality Characteristics and Performance






“Performance is only loosely related to code speed”. To the extent
that you work on your code’s speed, you’re not working on other
quality characteristics.
work on speed may hurt performance rather than help it

Performance and Code Tuning


When efficiency is chosen as a priority - think about efficiency from
each of these view points








Program Requirements
Program Design
Class and Routine Design
Operating-System Interactions
Code Compilation
Hardware
Code Tuning

Prof. Asha N

3
2. Introduction to Code Tuning


Code Tuning - mastering the art of writing
efficient code, One attraction is that a routine
that executes in 20 microseconds, tweak a
few lines, and reduce the execution speed to
2 microseconds

Prof. Asha N

4
Contd…


The Pareto Principle




The Pareto Principle also known as the 80/20 rule, states
that you can get 80 percent of the result with 20 percent of
the effort. The principle applies to a lot of areas other than
programming, but it definitely applies to program
optimization.

Old Wives’ Tales


some common misapprehensions about code tuning


Reducing the lines of code in a high-level language
improves the speed or size of the resulting machine
code—false!

Prof. Asha N

5
Contd….
Consider the following code that initializes a 10-element
array:
(a) for i = 1 to 10
a[ i ] = I
end for
(b) 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

Prof. Asha N

6
Contd….


Certain operations are probably faster or smaller than
others—false!




What was true on one machine with one set of tools can easily be
false on another machine with a different set of tools.

You should optimize as you go—false!


Focusing on optimization during initial development detracts from
achieving other program objectives.



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



When to Tune





It’s hardly ever true that programs need to be fast or small before
they need to be correct

Use a high-quality design. Make the program right. Make it
modular and easily modifiable so that it’s easy to work on later.
When it’s complete and correct, check the performance. If the
program lumbers, make it fast and small. Don’t optimize until you
know you need to.

Prof. Asha N

7
Contd….


Compiler Optimizations

Prof. Asha N

8
3. Kinds of Fat and Molasses




In code tuning you have to profile the
program to know with any confidence which
parts are slow and fat
Common Sources of Inefficiency







Input/output operations
Paging
System calls
Interpreted languages
Errors
Prof. Asha N

9
4. Measurement




small parts of a program usually consume a
disproportionate share of the run time, So
measure your code to find the hot spots.
Once you’ve found the hot spots and
optimized them, measure the code again to
assess how much you’ve improved it

Prof. Asha N

10
Contd…


C++ Example of Straightforward Code to
Sum the Elements in a Matrix
sum = 0;
for ( row = 0; row < rowCount; row++ ) {
for ( column = 0; column < columnCount; column++ ) {
sum = sum + matrix[ row ][ column ];
}
}

Prof. Asha N

11
Contd…


C++ Example of an Attempt to Tune Code
to sum the Elements in a Matrix
sum = 0;
elementPointer = matrix;
lastElementPointer = matrix[ rowCount - 1 ][ columnCount - 1 ] + 1;
while ( elementPointer < lastElementPointer ) {
sum = sum + *elementPointer++;
}

Prof. Asha N

12
5. Iteration


keep trying until u improve to an extent.

Prof. Asha N

13
Contd….

Prof. Asha N

14

More Related Content

PDF
Code tuning techniques
PPT
Code Tuning
PPTX
Pseudocode flowcharts
PPTX
Control Function - Computer Architecture
PPTX
Flowchart and algorithem
PPTX
Algorithm analysis and efficiency
PPTX
Unit 1. Problem Solving with Computer
PPTX
Computational Complexity
Code tuning techniques
Code Tuning
Pseudocode flowcharts
Control Function - Computer Architecture
Flowchart and algorithem
Algorithm analysis and efficiency
Unit 1. Problem Solving with Computer
Computational Complexity

What's hot (20)

PPT
pseudo code basics
PPT
asymptotic notations i
PDF
Introduction to algorithms
PDF
Algorithm and Programming (Introduction of Algorithms)
DOCX
Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)
PDF
Algorithms Lecture 4: Sorting Algorithms I
PDF
Algorithms Lecture 3: Analysis of Algorithms II
PPTX
Big o notation
PPTX
Code generation
PPTX
Insertion Sorting
PPT
Unit 1 chapter 1 Design and Analysis of Algorithms
PPTX
What is an algorithm?
PPT
Turing Machine
PDF
Garbage Collection
PPTX
Three address code In Compiler Design
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PPT
Time complexity
PPTX
Presentation on computer language
PPT
Horspool Algorithm in Design and Analysis of Algorithms in VTU
PPTX
Pseudocode
pseudo code basics
asymptotic notations i
Introduction to algorithms
Algorithm and Programming (Introduction of Algorithms)
Problem solving UNIT - 4 [C PROGRAMMING] (BCA I SEM)
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 3: Analysis of Algorithms II
Big o notation
Code generation
Insertion Sorting
Unit 1 chapter 1 Design and Analysis of Algorithms
What is an algorithm?
Turing Machine
Garbage Collection
Three address code In Compiler Design
DESIGN AND ANALYSIS OF ALGORITHMS
Time complexity
Presentation on computer language
Horspool Algorithm in Design and Analysis of Algorithms in VTU
Pseudocode
Ad

Viewers also liked (18)

PPTX
Variables
PPT
代码大全(内训)
PDF
Defencive programming
PDF
Coding Style
PDF
MOST_OpenFoundry_version control system_Git
PPT
A Guideline to Test Your Own Code - Developer Testing
PDF
高品質軟體的基本動作 101 + 102 for NUU
PPTX
程序员实践之路
PDF
Design in construction
PDF
Design in construction
PDF
程序员发展漫谈
PDF
Integration
PPTX
Java scriptcore brief introduction
PDF
高品質軟體的基本動作 101 for NTHU
PDF
The pseudocode
PPTX
Code optimization
PPT
Rm 1 Intro Types Research Process
Variables
代码大全(内训)
Defencive programming
Coding Style
MOST_OpenFoundry_version control system_Git
A Guideline to Test Your Own Code - Developer Testing
高品質軟體的基本動作 101 + 102 for NUU
程序员实践之路
Design in construction
Design in construction
程序员发展漫谈
Integration
Java scriptcore brief introduction
高品質軟體的基本動作 101 for NTHU
The pseudocode
Code optimization
Rm 1 Intro Types Research Process
Ad

Similar to Code tuning strategies (20)

PPTX
CODE TUNINGtertertertrtryryryryrtytrytrtry
PPTX
Programming C ppt for learning foundations
PPT
Software development slides
PDF
L1. Basic Programming Concepts.pdf
PPT
Cs 568 Spring 10 Lecture 5 Estimation
DOCX
Chapter 2(1)
PDF
Introduction to Computer Programming
PPTX
Module 1 python.pptx
PDF
TOPIC-1-Introduction and Preliminaries.pdf
PPTX
PCCF UNIT 1.pptx
PDF
L5555555555555555555555 Agile Scrum Framework.pdf
DOC
Stnotes doc 5
PDF
Qualidade de Software em zOS usando IBM Debug Tool e RDz
PPTX
C++ good tutorial
PPTX
Software cost estimation
PPTX
Programming _Language of Logic_ PPT.pptx
PPTX
Programming str_Language of Logic/c.pptx
PPT
PPTX
Improving Code Quality Through Effective Review Process
CODE TUNINGtertertertrtryryryryrtytrytrtry
Programming C ppt for learning foundations
Software development slides
L1. Basic Programming Concepts.pdf
Cs 568 Spring 10 Lecture 5 Estimation
Chapter 2(1)
Introduction to Computer Programming
Module 1 python.pptx
TOPIC-1-Introduction and Preliminaries.pdf
PCCF UNIT 1.pptx
L5555555555555555555555 Agile Scrum Framework.pdf
Stnotes doc 5
Qualidade de Software em zOS usando IBM Debug Tool e RDz
C++ good tutorial
Software cost estimation
Programming _Language of Logic_ PPT.pptx
Programming str_Language of Logic/c.pptx
Improving Code Quality Through Effective Review Process

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Pharma ospi slides which help in ospi learning
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
RMMM.pdf make it easy to upload and study
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Complications of Minimal Access Surgery at WLH
Pharma ospi slides which help in ospi learning
Classroom Observation Tools for Teachers
Cell Structure & Organelles in detailed.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
STATICS OF THE RIGID BODIES Hibbelers.pdf
VCE English Exam - Section C Student Revision Booklet
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
RMMM.pdf make it easy to upload and study
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

Code tuning strategies

  • 1. Code-Tuning Strategies Code Complete Author : Steven C. McConnell. Prof. Asha N 1
  • 2. Code-Tuning Strategies 1. 2. 3. 4. 5. Performance Overview Introduction to Code Tuning Kinds of Fat and Molasses Measurement Iteration Prof. Asha N 2
  • 3. 1. Performance Overview  Code tuning is one way of improving a program’s performance  Quality Characteristics and Performance    “Performance is only loosely related to code speed”. To the extent that you work on your code’s speed, you’re not working on other quality characteristics. work on speed may hurt performance rather than help it Performance and Code Tuning  When efficiency is chosen as a priority - think about efficiency from each of these view points        Program Requirements Program Design Class and Routine Design Operating-System Interactions Code Compilation Hardware Code Tuning Prof. Asha N 3
  • 4. 2. Introduction to Code Tuning  Code Tuning - mastering the art of writing efficient code, One attraction is that a routine that executes in 20 microseconds, tweak a few lines, and reduce the execution speed to 2 microseconds Prof. Asha N 4
  • 5. Contd…  The Pareto Principle   The Pareto Principle also known as the 80/20 rule, states that you can get 80 percent of the result with 20 percent of the effort. The principle applies to a lot of areas other than programming, but it definitely applies to program optimization. Old Wives’ Tales  some common misapprehensions about code tuning  Reducing the lines of code in a high-level language improves the speed or size of the resulting machine code—false! Prof. Asha N 5
  • 6. Contd…. Consider the following code that initializes a 10-element array: (a) for i = 1 to 10 a[ i ] = I end for (b) 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 Prof. Asha N 6
  • 7. Contd….  Certain operations are probably faster or smaller than others—false!   What was true on one machine with one set of tools can easily be false on another machine with a different set of tools. You should optimize as you go—false!  Focusing on optimization during initial development detracts from achieving other program objectives.  A fast program is just as important as a correct one—false!  When to Tune   It’s hardly ever true that programs need to be fast or small before they need to be correct Use a high-quality design. Make the program right. Make it modular and easily modifiable so that it’s easy to work on later. When it’s complete and correct, check the performance. If the program lumbers, make it fast and small. Don’t optimize until you know you need to. Prof. Asha N 7
  • 9. 3. Kinds of Fat and Molasses   In code tuning you have to profile the program to know with any confidence which parts are slow and fat Common Sources of Inefficiency      Input/output operations Paging System calls Interpreted languages Errors Prof. Asha N 9
  • 10. 4. Measurement   small parts of a program usually consume a disproportionate share of the run time, So measure your code to find the hot spots. Once you’ve found the hot spots and optimized them, measure the code again to assess how much you’ve improved it Prof. Asha N 10
  • 11. Contd…  C++ Example of Straightforward Code to Sum the Elements in a Matrix sum = 0; for ( row = 0; row < rowCount; row++ ) { for ( column = 0; column < columnCount; column++ ) { sum = sum + matrix[ row ][ column ]; } } Prof. Asha N 11
  • 12. Contd…  C++ Example of an Attempt to Tune Code to sum the Elements in a Matrix sum = 0; elementPointer = matrix; lastElementPointer = matrix[ rowCount - 1 ][ columnCount - 1 ] + 1; while ( elementPointer < lastElementPointer ) { sum = sum + *elementPointer++; } Prof. Asha N 12
  • 13. 5. Iteration  keep trying until u improve to an extent. Prof. Asha N 13