Performance W hen to worry, and when not to Strategies to boost - - PDF document

performance
SMART_READER_LITE
LIVE PREVIEW

Performance W hen to worry, and when not to Strategies to boost - - PDF document

Performance W hen to worry, and when not to Strategies to boost performance Source: J on Bentley, W riting Efficient Programs , Prentice-Hall 1982 Software Engineering, M Young 5/15/00 1 Three cardinal rules of optimization


slide-1
SLIDE 1

1 1

Software Engineering, M Young 5/15/00 1

Performance

  • W hen to worry, and when not to
  • Strategies to boost performance

Source: J

  • n Bentley, W riting Efficient Programs,

Prentice-Hall 1982

Software Engineering, M Young 5/15/00 2

Three cardinal rules of optimization

  • Don't do it
  • Don't do it yet
  • Don't do it here
slide-2
SLIDE 2

2 2

Software Engineering, M Young 5/15/00 3

Don't do it

H ow fast is fast enough?

  • There is no point in efficiency if

– The program will not take long to run – The program is run only occasionally – I/O will dominate computation

  • Programmers (including maintenance

programmers) cost more than computer time.

Software Engineering, M Young 5/15/00 4

Response time

Response is more important than efficiency for interactive programs.

  • 1/20 second is fast enough for echo.

– At around 1/10 second, delay becomes noticeable.

  • 1 second or less is bes

t for execution.

– At around 1 second, users' minds wander, accuracy and productivity fall off markedly.

slide-3
SLIDE 3

3 3

Software Engineering, M Young 5/15/00 5

Don't do it yet

  • If you do need to optimize, don't let it ruin

the design.

  • D o consider overall organization for

efficiency

  • Don't twiddle bits in high-level design
  • Use modular structure to hide the ug

ly parts.

Software Engineering, M Young 5/15/00 6

Don't do it here

  • 4% of the code takes 50% of the time.
  • You probably don't know which 4%.
slide-4
SLIDE 4

4 4

Software Engineering, M Young 5/15/00 7

Steps

  • If you expect performance to be a problem

– Fix the specification – Clean design with hidden decisions – Isolate bottlenecks – Revise data structures – Revise algorithms – Revise code

  • In that order!

Software Engineering, M Young 5/15/00 8

Specification

Specification is the point of greates t leverage.

A specification shouldn't prescribe an implementation, but it must allow an efficient implementation. Use back-of-the-envelope calculations to determine feasibility.

  • Examples

– Exact vs. approximate solution – Incremental vs. batch solution – Available information

slide-5
SLIDE 5

5 5

Software Engineering, M Young 5/15/00 9

Example --- lev erage in the spec

  • Spec of spell program: Must reject all

mispelled words, accept all words in dictionary.

  • Revised spec: Must reject nn% of

mispelled words, including 100% of the nn most commonly mispelled words.

  • Revised spec allows bit table

implementation with stop list.

Software Engineering, M Young 5/15/00 10

Efficiency in modular decomposition

  • Identify critical activities
  • Localize critical representation decisions
  • Allow flexibility within modules

– Example: Timestamp logging was slower than locking in the RAID database system. Profiling revealed a bottleneck in a linked list traversal. The list was hidden in a C++ class, so changing a few lines of code replaced the list by an AVL tree, and timestamp logging became faster than locking.

slide-6
SLIDE 6

6 6

Software Engineering, M Young 5/15/00 11

Bottlenecks

  • According to Knuth: 4% of the code takes

95% of the time.

  • According to B

entley: Programmers almost always guess the wrong 4%

If a program is too slow, it must be monitored.

Software Engineering, M Young 5/15/00 12

M onitoring

  • Code can be instrumented
  • Code can be profiled

– Ex. gprof in Unix, profiling tools with compiler, network monitoring tools, …

  • Profile large or typical problems
slide-7
SLIDE 7

7 7

Software Engineering, M Young 5/15/00 13

Aside --- profiling techniques

Probes: Read clock and/or bump counters at each procedure call and return. Interrupts: Interrupt program at regular intervals and inspect call-stack.

  • Interrupt-driven profiling is generally more

accurate, but probes are easier to implement in a hurry

  • Use “virtual clock” or else keep probe
  • verhead very small.

Software Engineering, M Young 5/15/00 14

W hat to do with a bottleneck

  • Look not only at the bottleneck, but how it

is used.

  • Is it dominated by a few common cases?
  • Can the common case be solved another

way?

slide-8
SLIDE 8

8 8

Software Engineering, M Young 5/15/00 15

Example --- memory allocation/free as bottleneck

  • O ften dominated by a few small sizes
  • Sometimes ``almost LIFO ''
  • O ptimize the common cases, not the

general case:

– van W yck, W ulf: Free lists for small, common sizes; general pool for larger requests. – Hanson: ``Arenas'' with mark/release semantics.

Software Engineering, M Young 5/15/00 16

Big-oh vs. bit-twiddles

  • A Commodore 64 searches a tree faster

than a Cray 2 searches a linear table—if the table is larg e enough.

– For large problems, a better data structure or algorithm is usually the answer.

  • Sometimes us

e mixed strategy: ex., Q uicksort with insertion sort for small lists

slide-9
SLIDE 9

9 9

Software Engineering, M Young 5/15/00 17

Storing precomputed results

  • Actually an instance of a more general rule:

M ove calculations out of a loop. – In this case the ``loop'' is program execution.

  • Build a table once (with another program)

and use it over and over. Examples:

– Trig interpolation table (or any other smooth function) – Keyword hash table for compiler/interpreter

Software Engineering, M Young 5/15/00 18

Data structure augmentation

  • It is often worthwhile to have two versions
  • f an algorithm.

– Version one works fast, usually; but sometimes it doesn't work. – Version two works slowly, always.

  • Examples:
  • Inaccurate file index
  • Hash signature augments full values

(quick inequality check, equality is slower)

slide-10
SLIDE 10

10 10

Software Engineering, M Young 5/15/00 19

Caching

  • Most commonly requested data should be

fastest to produce.

  • Examples:

– Self-organizing lists (move-to-front) – Bloom filters – Function result caching – ``cat'' pages vs. ``man'' pages in Unix – Caching proxy servers for W W W

Software Engineering, M Young 5/15/00 20

Example: Font caching

  • In systems like PostScript, characters can

be arbitrarily scaled and rotated.

– High-quality, scalable fonts are described by geometry, not bitmaps. – Drawing each character would be too slow. – Solution: Fonts are cached in the printer --- second page is usually faster than the first!

slide-11
SLIDE 11

11 11

Software Engineering, M Young 5/15/00 21

Space optimization --- indirection

  • Example: Colormaps

save space and time.

– Each pixel contains a colormap index. – Colormap is a table; entries contain RGB values. – S avings ~ 24 - 8 = 16 bits per pixel – Faster to store and read (memory bandwidth limitation). – Sometimes used for fast animation effects.

Software Engineering, M Young 5/15/00 22

Suggested reading

W riting Efficient Programs, by J

  • n Louis Bentley. Prentice-

Hall, 1982.

``The techniques of writing efficient code are in many ways like a

snakebite kit: used in the right context, they can be just the thing for the job at hand, but their inappropriate applica tion can be

  • disastrous. Unfortunately, most programmers like to play with

new toys. I have ma ny friends who, immedia tely upon buying a snakebite kit, would be tempted to throw the first person they see to the ground, tie the tourniquet on him, slash him with the knife, and apply suction to the wound. W hat that action does to people, you might be tempted to do to software systems by haphaza rdly applying the techniques of this book.''