tori bowman csse 375, rose-hulman october 22, 2007 1 code tuning (chapters 25-26 of code complete)

25
Tori Bowman CSSE 375, Rose- Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Upload: erin-edmunds

Post on 30-Mar-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Tori Bowman

CSSE 375, Rose-Hulman

October 22, 2007

1

Code Tuning(Chapters 25-26 of

Code Complete)

Page 2: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Outline

2

State of 375This

Code Tuning StrategiesCode Tuning Techniques

Page 3: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

State of 375

3

TonightHW 5 due 11:55 pm

Tomorrow: part lecture/part work timeThursday:

Week one progress reportFriday:

In class work time

Page 4: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

4

Code Tuning Strategies

Page 5: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Overview

5

Code-tuning is one method of improving a program’s performance through various techniques

Performance is loosely related to speedWhen you work on speed, you’re not working

on other quality attributes

Page 6: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Historical context

6

1960s:Resources severely limitedNothing more important than efficiency

1970s:Computing improvesRealizing how much it hurt readability and

maintainability1980s:

Microcomputers brought efficiency issues backWaned in throughout the 1990s

2000s:Memory in embedded issues becomes the

efficiency concern

Page 7: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Efficiency viewpoints

7

Program requirementsProgram designClass and routine designOperating-system interactionsCode compilationHardwareCode tuning

Page 8: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Program requirements

8

Barry Boehm’s example from TRWInitial requirement: sub-second response

timeHighly complex designEstimate cost: $100 million

Further analysis:User’s happy with four-second response time

90% of the timeReduced system cost by ~$70 million

Make sure you’re solving a problem that needs to be solved

Page 9: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Program design

9

Set overall system goalThen set subsystems, features, classes

Helps to identify troublesome subsystemsMaking the objectives explicit improves the

likelihood they’ll be achieved

Page 10: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Code Tuning

10

Not the most effectiveNot the easiestNot always the cheapest

So…. why do it?Personal satisfaction?Rite of passage?

Page 11: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Pareto Principle

11

Also known as the 80/20 rule80 percent of the result is achieved by 20

percent of the effort

“An Empirical Study of Fortran Programs” by Donald Knuth< 4% of a program accounts for more than

50% of it’s run-time

How does this relate to code tuning?

Page 12: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Let’s consider…

12

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

Page 13: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Which is faster?

13

for i = 1 to 10a[ i ] = i

end for

a [ 1 ] = 1a [ 2 ] = 2a [ 3 ] = 3a [ 4 ] = 4a [ 5 ] = 5a [ 6 ] = 6a [ 7 ] = 7a [ 8 ] = 8a [ 9 ] = 9a [ 10 ] = 10

Page 14: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Results…

14

Languagefor-Loop

TimeStraight-code time

Time Savings

Performance Ratio

Visual Basic 8.47 3.16 63% 2.5:1

Java 12.6 3.23 74% 4:1

Page 15: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Common sources of inefficiencies

15

I/O OperationsPagingSystem CallsInterpreted languagesErrors

Page 16: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

16

Code Tuning Techniques

Page 17: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

CotD

17

Page 18: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Logic

18

Stop testing when you know the answerShort circuit in conditionals and loopsStandard with C++ and Java

Order tests by frequencyCompare performance of similar logic

structuresSome languages case is faster than if-then-else

Substitute table lookups for complicated expressions

Page 19: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Loops - unswitching

19

Make decisions outside of the loop when possibleie. Putting the loop inside the conditionalGood for ~20% time savings

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

Page 20: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

More loops

20

JammingCombine loops that operate over the same

elementsUnrolling

Reduce the amount of loop housekeepingMinimize work inside of the loop

Assign complicated pointer expressions to a well-named variable

Page 21: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

More loops cont.

21

Sentinel valuesSimplifying compound tests

Nested loops:Put busiest loop on the inside

Strength reductionReplace expensive operations (multiplication)

with cheaper operations (addition)

Page 22: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Data transformations

22

Use integers rather than floating-point numbers

Use the fewest array dimensions possibleMinimize array referencesUse supplementary indexesUse caching

Page 23: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Expressions

23

Exploit algebraic identitiesnot a and not b vs. not (a or b)

Strength reductionInitialize at compile time

More in the book…

Page 24: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

Read the book for…

24

RoutinesRecoding in a low-level languageChecklistsAdditional resources

Page 25: Tori Bowman CSSE 375, Rose-Hulman October 22, 2007 1 Code Tuning (Chapters 25-26 of Code Complete)

25

Questions?