back of envelope

5
8/19/2019 Back of Envelope http://slidepdf.com/reader/full/back-of-envelope 1/5 by Jon Bentley prog ramming pearls THE B CK OF THE ENVELOPE It was in the middle of a fascinating conversation on software engineering that Bob Martin asked me, "How much water flows out of the Mississippi River in a day?" Because I had found his comments up to that point deeply insightful, I politely stifled my true re- sponse and said, "Pardon me?". When he asked again I realized that I had no choice but to humor the poor fellow, who had obviously cracked under the pressures of running a large software shop within AT&T Bell Lab- oratories. My response went something like this. I figured that near its mouth the river was about a mile wide and maybe 20 feet deep (or about one two-hundred-and- fiftieth of a mile). I guessed that the rate of flow was five miles an hour, or 120 miles per day. Multiplying 1 x 1/250 x 120 showed that the river discharged about half a cubic mile of water per day, to within an order of magnitude. 1 But so what? At that point Martin picked up a proposal on his desk for the development of a large computer-based mail system, and went through a similar sequence of calcu- lations. Although his numbers were more precise (they were straight from the proposal), the calculations were just as simple and much more revealing. They showed that, under generous assumptions, the proposed system had a chance of working only if there were at least a hundred and twenty seconds in each minute. He had sent the design back to the drawing board the previous day. That was Bob Martin's wonderful (if eccentric) way of introducing the engineering technique of "back-of- the-envelope" calculations. The technique is standard fare in most engineering curricula, and is bread and butter for practicing engineers in many fields. Unfortu- nately, it is too often neglected in computing. Quick Calculations Card, Moran, and Newell paint an ambitious picture on 1 When I asked Peter Weinberger how much water flows out of the Mississippi per day, he responded, As much as flows in." He then estimated that the Mississippi basin was about 1000 by 1000 miles, and the annual rainfall there was about one foot (or one five-thousandth of a mile). Multiplying 1000 x 1000 x 1/5 000 gave 200 cubic miles per year. or a little more than half a cubic mile per day. It's important to double check all calculations, and especially so for quick ones. As a (cheating) triple check, an almanac reported that the river's discharge is 640,000 cubic feet per second, or about 0.4 cubic miles per day. The proxi mity of the two estimates to one another, and especially to the almanac's an- swer, is a fine example of sheer dumb luck. © 1984 ACM 0001-0782/84/0300-0180 75¢ pages 9 and 10 of their Psychology of Human-Computer Interaction (published in 1983 by Lawrence Erlbaum As- sociates, Publishers, of Hillsdale, New Jersey, and Lon- don, England; this excerpt is reprinted with the kind permission of the publisher). A system designer, the head of a small team writ- ing the specifications for a desktop calendar-sched- uling system, is choosing between having users type a key for each command and having them point to a menu with a lightpen. On his white- board, he lists some representative tasks users of his system must perform. In two columns, he writes the steps needed by the "key-command" and "menu" options. From a handbook, he culls the times for each step, adding the step times to get total task times. The key-command system takes less time, but only slightly. But, applying the analy- sis from another section of the handbook, he calcu- lates that the menu system will be faster to learn; in fact, it will be learnable in half the time. He has estimated previously that an effective menu system will require a more expensive processor: 20% more memory, 100% more microcode memory, and a more expensive display. Is the extra expenditure worthwhile? A few more minutes of calculation and he realizes the startling fact that, for the manu- facturing quantities now anticipated, training costs for the key-command system will exceed unit manufacturing costs The increase in hardware costs would be much more than balanced by the decrease in training costs, even before considering the increase in market that can be expected for a more easily learned system. Are there advantages to the key-command system in other areas, which need to be balanced? He proceeds with other anal- yses, considering the load on the user's memory, the potential for user errors, and the likelihood of fatigue. In the next room, the Pascal compiler hums idly, unused, awaiting his decision. Their book then goes on to develop a scientific base in psychology that is a necessary precursor to such a handbook. That scenario shows how a few envelopes' worth of arithmetic might enable a system designer to make a rational choice between two appealing alternatives. That is a fundamentally different use than Martin's, his 18 Communications f the ACM March 1984 Volume 27 Number 3

Upload: geouno

Post on 08-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Back of Envelope

8/19/2019 Back of Envelope

http://slidepdf.com/reader/full/back-of-envelope 1/5

b y J o n B e n t l e y

p r o g r a m m in g

pearls

THE B CK OF THE ENVELOPE

It was in the middle of a fascinating conversation on

software engineering that Bob Martin asked me, "How

muc h wate r flows out of the Mississippi River in a

day?" Because I had found his comm ents up to that

point deeply insightful, I politely stifled my true re-

sponse and said, "Pardon me?". When he asked again I

realized that I had no choice but to humor the poor

fellow, who had obviously cracked under the pressures

of running a large software shop within AT&T Bell Lab-

oratories.

My response went s ometh ing like this. I figured that

near its mouth the river was about a mile wide and

maybe 20 feet deep (or about one two- hundred-an d-

fiftieth of a mile). I guessed that the rate of flow was

five miles an hour, or 120 miles per day. Multiplying

1 x 1/250 x 120 showed that the river discharged

about half a cubic mile of water per day, to within an

order of magnitude. 1 But so what?

At that point Martin picked up a proposal on his desk

for the development of a large computer -based mail

system, and went through a similar sequence of calcu-

lations. Although his numbe rs were more precise (they

were straight from the proposal), the calculations were

just as simple and muc h more revealing. They show ed

that, under generous assumptions, the proposed system

had a chance of working only if there were at least a

hundred and twenty seconds in each minute. He had

sent the design back to the drawing board the previous

day.

That was Bob Martin's wonderful (if eccentric) way

of introducing the engineering technique of "back-of-

the-envelope" calculations. The technique is standard

fare in most engineering curricula, and is bread and

butter for practicing engineers in many fields. Unfortu-

nately, it is too often neglected in computing.

Quick Calculations

Card, Moran, and Newell paint an ambitious picture on

1 When I asked Peter Weinb erger how much water flows out of the

Mississippi per day, he responded, As much as flows in." He then

estimated that the Mississippi basin was about 1000 by 1000 miles, and

the annu al rainfall there was about one foot (or one five-thou sandth of a

mile). Multiplyi ng 1000 x 1000 x 1/5 000 ga ve 200 cubic miles per year.

or a little more than half a cubic mile pe r day. It's important to double

check all calculations, and especially so for quick ones. As a (cheating)

triple check, an almana c reported that the river's discharge is 640,000

cubic feet per second, or about 0.4 cubic miles per day. The proxi mity of

the two estimates to one another, and especially to the almanac's an-

swer, is a fine examp le of sheer dum b luck.

© 1984 ACM 0001-0782/84/0300-0180 75¢

pages 9 and 10 of their

P s y c h ol o g y o f H u m a n - C o m p u t e r

In te rac t ion

(published in 1983 by Lawrence Erlbaum As-

sociates, Publishers, of Hillsdale, New Jersey, and Lon-

don, England; this excerpt is reprinted with the kind

permission of the publisher).

A system designer, the head of a small team writ-

ing the specifications for a desktop calendar-sched-

uling system, is choosing between having users

type a key for each command and having them

point to a menu with a lightpen. On his white-

board, he lists some representative tasks users of

his system must perform. In two columns, he

writes the steps needed by the "key-command"

and "menu" options. From a handbook, he culls

the times for each step, add ing the step times to get

total task times. The key-c omman d system takes

less time, but only slightly. But, applying the analy-

sis from another section of the handbook, he calcu-

lates that the menu system will be faster to learn;

in fact, it will be learnable in half the time. He has

estimated previously that an effective menu system

will require a more expensive processor: 20% more

memory, 100% more microcode memory, and a

more expensive display. Is the extra expenditure

worthwhile? A few more minutes of calculation

and he realizes the startling fact that, for the manu-

facturing quantities now anticipated, training costs

for the ke y-comm and system will exceed unit

manufactur ing costs The increase in hardware

costs would be much more than balanced by the

decrease in training costs, even before considering

the increase in mar ket that can be expected for a

more easily learned system. Are there advantages

to the key-command system in other areas, which

need to be balanced? He proceeds with other anal-

yses, considering the load on the user's memory,

the potential for user errors, and the likelihood of

fatigue. In the next room, the Pascal compiler

hums idly, unused, awaiting his decision.

Their book then goes on to develop a scientific base in

psychology that is a necessary pr ecursor to such a

handbook.

That scenario shows how a few envelopes' worth of

arithmetic might enable a system designer to make a

rational choice between two appealing alternatives.

That is a fundamentally different use than Martin's, his

18

C o m m u n i c a ti o n s f t he A C M M a r ch 1 9 8 4 V o l u m e 27 Numb er 3

Page 2: Back of Envelope

8/19/2019 Back of Envelope

http://slidepdf.com/reader/full/back-of-envelope 2/5

P r o g r a m m i n g P e a r l s

a n a l y s i s o f a s i n g l e d e s i g n u n c o v e r e d a f a t a l f l a w . I n

b o t h c a s e s , a s h o r t s e q u e n c e o f c a l c u l a t i o n s w a s s u f f i-

c i e n t t o a n s w e r t h e q u e s t i o n a t h a n d ; a d d i t i o n a l f i g u r -

i n g w o u l d h a v e s h e d l i t t l e l i g h t.

E a r l y i n t h e d e s i g n o f a s y s t e m , r a p i d c a l c u l a t i o n s

c a n s t e e r t h e d e s i g n e r a w a y f r o m d a n g e r o u s w a t e r s i n t o

s a f e p a s s a g e s . A n d i f y o u d o n ' t u s e t h e m e a r l y , t h e y

m a y s h o w i n r e t r o s p e c t t h a t a p r o j e c t w a s d o o m e d t o

f a i lu r e . T h e c a l c u l a t i o n s a r e u s u a l l y t r i v i al , e m p l o y i n g

n o m o r e t h a n h i g h s c h o o l m a t h e m a t i c s ; t h e h a r d p a r t i s

r e m e m b e r i n g t o a p p l y t h e m e a r l y e n o u g h i n t h e l i fe o f

a s o f t w a r e p r o j e c t .

S a f e t y F a c t o r s

T h e o u t p u t o f a q u i c k c a l c u l a t i o n i s o n l y a s g o o d as i ts

i n p u t . S o m e t i m e s s l o p p y i n p u t i s e n o u g h t o ge t i n to t h e

r i g ht b a l l p a r k : if y o u g u e s s a b o u t t w e n t y p e r c e n t h e r e

a n d f i ft y p e r c e n t t h e r e a n d s t i l l f i n d t h a t a d e s i g n i s a

h u n d r e d t i m e s a b o v e o r b e l o w s p e c i f ic a t i o n , a d d i t i o n a l

a c c u r a c y i s n ' t n e e d e d . W i t h a c c u r a t e d a t a , t h o u g h ,

b a c k - o f - t h e - e n v e l o p e c a l c u l a t i o n s c a n y i e l d a c c u r a t e

a n s w e r s . 2 B e f o r e p l a c i n g t o o m u c h c o n f i d e n c e i n a

t w e n t y p e r c e n t m a r g i n , c o n s i d e r V i c V y s s o t s k y 's a d v i c e

( f r o m a t a l k h e h a s g i v e n o n s e v e r a l o c c a s i o n s ) .

M o s t o f y o u , s a y s V y s s o t s k y , p r o b a b l y r e c a l l p i c -

t u r e s o f ' G a l l o p i n g G e r t i e , ' t h e T a c o m a N a r r o w s b r i d g e

w h i c h t o r e i t s e l f a p a r t i n a w i n d s t o r m i n 1 9 40 . 3 W e l l ,

s u s p e n s i o n b r i d g e s h a d b e e n r i p p i n g t h e m s e l v e s a p a r t

t h a t w a y f o r 80 y e a r s o r s o b e f o r e G a l l o p i n g G e r t i e . I t ' s

a n a e r o d y n a m i c l i f t p h e n o m e n o n , a n d t o d o a p r o p e r

e n g i n e e r i n g c a l c u l a t i o n o f th e f o rc e s, w h i c h i n v o l v e

d r a s t i c n o n l i n e a r it i e s , y o u h a v e t o u s e t h e m a t h e m a t i c s

a n d c o n c e p t s o f K o l m o g o r o v to m o d e l t h e e d d y s p e c -

t r u m . N o b o d y r e a l l y k n e w h o w t o d o t h is c o r r e c t l y in

d e t a i l u n t i l t h e 1 9 5 0 s o r t h e r e a b o u t s . S o , w h y h a s n ' t t h e

B r o o k l y n B r i d g e t o r n i t s e l f a p a r t , l i k e G a l l o p i n g G e r t i e ?

I t ' s b e c a u s e J o h n R o e b l i n g h a d s e n s e e n o u g h t o

k n o w w h a t h e didn't k n o w . H i s n o t e s a n d l e t t e r s o n t h e

d e s i g n o f t h e B r o o k l y n B r i d g e s t il l e x i s t , a n d t h e y a r e a

f a s c i n a t in g e x a m p l e o f a g o o d e n g i n e e r r e c o g n i z i n g t h e

l i m i t s o f h i s k n o w l e d g e . H e k n e w a b o u t a e r o d y n a m i c

l i ft o n s u s p e n s i o n b r i d g e s ; h e h a d w a t c h e d i t. A n d h e

k n e w h e d i d n ' t k n o w e n o u g h t o m o d e l i t. S o h e d e -

s i g n e d t h e s t i f f n e s s o f t h e t r u s s o n t h e B r o o k l y n B r i d g e

r o a d w a y t o b e

six times

w h a t a n o r m a l c a l c u l a t i o n

b a s e d o n k n o w n s t a t i c a n d d y n a m i c l o a d s w o u l d h a v e

c a l l e d f o r. A n d , h e s p e c i f i e d a n e t w o r k o f d i a g o n a l s t a y s

r u n n i n g d o w n t o th e r o a d w a y , t o st i ff e n th e e n t i r e

b r i d g e s t r u c tu r e . G o l o o k at t h o s e s o m e t i m e ; t h e y ' r e

a l m o s t u n i q u e .

2 Somet imes accur ate calc ulations are quit e useful. In 1969 Don Knuth

wrote a disk sorting package, only to find that it took twice the time

predicted by his calculations. Diligent checking uncovered the flaw: due

to a software bug. the system's one-year-old disks had run at only half

their advertised speed for their entire lives. When the bug was fixed, the

sorting package behaved as predicted and many other programs also ran

faster.

3 For more info rmation on the event, see Section 2.6.1 of Braun's Differ-

ential Equations and Their Applications. Second Edition. published in 1978

by Springer-Verlag.

W h e n R o e b l i n g w a s a s k e d w h e t h e r h i s p r o p o s e d

b r i d g e w o u l d n ' t c o l l a p s e l i k e s o m a n y o t h e r s, h e s a i d ,

' N o , b e c a u s e I d e s i g n e d i t s i x t i m e s a s s t r o n g a s i t n e e d s

t o b e , to p r e v e n t t h a t f r o m h a p p e n i n g . '

R o e b l i n g w a s a g o o d e n g i n e e r , a n d h e b u i l t a g o o d

b r i d g e, b y e m p l o y i n g a h u g e s a f e ty f a c t o r t o c o m p e n -

s a t e f o r h i s i g n o r a n c e . D o w e d o t h a t ? I s u b m i t t o y o u

t h a t i n c a l c u l a t i n g p e r f o r m a n c e o f o u r r e a l - t i m e s o f t-

w a r e s y s t e m s w e o u g h t t o d e r a t e t h e m b y a f ac t o r o f

t w o , o r f o u r , o r s i x , t o c o m p e n s a t e f o r o u r i g n o r a n c e . I n

m a k i n g r e l i a b i l i t y / a v a i l a b i l i t y c o m m i t m e n t s , w e o u g h t

t o s t a y b a c k f r o m t h e o b j e c t i v e s w e

think

w e c a n m e e t

b y a f a c to r o f t e n , t o c o m p e n s a t e f o r o u r i g n o r a n c e . I n

e s t i m a t i n g s iz e a n d c o s t a n d s c h e d u l e , w e s h o u l d b e

c o n s e r v a t i v e b y a f a c t o r o f t w o o r f o u r t o c o m p e n s a t e

f or o u r i g n o r an c e . W e s h o u l d d e s i g n t h e w a y J o h n

R o e b l i n g d i d , a n d n o t t h e w a y h i s c o n t e m p o r a r i e s d i d - -

s o f a r a s I k n o w , n o n e o f t h e s u s p e n s i o n b r i d g e s b u i l t b y

R o e b l i n g ' s c o n t e m p o r a r i e s i n t h e U n i t e d S t a t e s s ti l l

s t a n d s , a n d a q u a r t e r o f a l l t h e b r i d g e s o f a n y t y p e b u i l t

i n t h e U .S . in t h e 1 8 7 0 s c o l l a p s e d w i t h i n t e n y e a r s o f

c o n s t r u c t i o n .

A r e w e e n g i n e e r s , l i k e J o h n R o e b l i n g ? I w o n d e r .

A C a s e S t u d y

T o m a k e t h e a b o v e p o i n t s m o r e c o n c r e t e , I ' ll d e s c r i b e

h o w I ( a l m o s t ) u s e d t h e m i n a s y s t e m I b u i l t f o r a s m a l l

c o m p a n y . I ' v e g i v e n t h e d e t a i l s in C a r n e g i e - M e l l o n

U n i v e r s i t y C o m p u t e r S c i e n c e T e c h n i c a l R e p o r t C M U -

C S - 8 3 -1 0 8 ; I 'l l j u s t s k e t c h t h e m h e r e .

T h e s y s t e m p r e p a r e d s e v e r a l r e p o r t s a d a y t o s u m m a -

r i z e t h e d a t a o n 1 , 0 0 0 8 0 - c o l u m n r e c o r d s ; t h e r e p o r t s

w e r e e a c h a b o u t 8 0 p a ge s l o ng . T h e s y s t e m ' s p r e d e c e s -

s o r r a n o n a l a r g e m a i n f r a m e ; m y t a s k w a s t o i m p l e -

m e n t a s i m i l a r s y s t e m o n a p e r s o n a l c o m p u t e r ( u s i n g

i n t e r p r e t e d B A S I C ) .

E a r l y i n t h e d e s i g n o f t h e s y s t e m I d i d s i m p l e c a l c u l a -

t i o n s t o m a k e s u r e t h a t t h e p e r s o n a l c o m p u t e r w a s u p

t o t h i s a p p l i c a t i o n . T h e s p a c e a n a l y s i s w a s s i m p l e : I

c a l c u l a t e d t h e s i z e o f t h e s e v e r a l l a r g e s t t a b l e s , a n d

f o u n d t h a t t h e y u s e d o n l y h a l f o f t h e 4 8 K b y t e s o f t h e

m a c h i n e . T h e t i m e a n a l y s i s i n v o l v e d t w o m a i n p h a s e s .

I d i d n ' t w o r r y m u c h a b o u t t h e t i m e t o r e a d t h e r e c o r d s

a n d b u i l d t h e t a b l e s u s e d b y t h e p r i n t p h a s e . I k n e w

t h a t a p r e v io u s s y s t e m d i d t h a t t a s k o n a n IB M S y s t e m /

3 60 M o d e l 2 5 i n a m i n u t e , a n d t h e m i c r o p r o c e s s o r o n

t h e p e r s o n a l c o m p u t e r i s m o r e p o w e r f u l t h a n t h a t o l d

w o r k h o r s e . I n s t ea d , I c o n c e n t r a t e d o n t h e t i m e t o p r i n t

t h e r e p o r t, w h i c h I t h o u g h t w o u l d b e l i m i t e d b y t h e

6 0 - l i n e s - p e r - m i n u t e s p e e d o f t h e p r i n t e r . E a c h p a g e o f

t h e r e p o r t c o n t a i n e d o n l y a b o u t 3 0 l in e s , so t h e t o t a l

t i m e o f 4 0 m i n u t e s w a s w e l l w i t h i n b o u n d s . A f t e r t h i s

s h o r t a n a l y si s , I p u r c h a s e d t h r e e p e r s o n a l c o m p u t e r s

a n d i m p l e m e n t e d t h e d e s i g n .

T h e f i rs t i m p l e m e n t a t i o n o f th e p r o g r a m w a s e n l i g h t -

e n i n g. S t o ri n g th e i n t e r p r e t e d p r o g r a m r e q u i r e d a b o u t

2 0 k i l o b y t e s of m a i n m e m o r y t h a t I h a d i g n o r e d i n m y

c a l c u l a t i o n ; t h e s a f e t y f a c t o r o f t w o s a v e d t h e d a y . T h e

c a l c u l a t i o n o f p r i n t i n g t i m e w a s r i g h t o n t h e m a r k ; i t

t o o k a b o u t 4 0 m i n u t e s . U n f o r t u n a t e l y , I w a s w a y o f f i n

March 198 4 Volume 27 Number 3 Communications of the ACM

181

Page 3: Back of Envelope

8/19/2019 Back of Envelope

http://slidepdf.com/reader/full/back-of-envelope 3/5

P rogram m ing P e ar l s

the time to read the records and build the table. Instead

of taking a minute, it took

1 4 h o u r s ,

which made it

awfully hard to prepare a few reports a day. The prob-

lem was that I had compared assembly code on the old

System/360 with interpreted BASIC on the personal

computer, ignoring the fact that i nterp reted BASIC usu-

ally runs several hundr ed times slower than assembly

code.

At that point I did a more careful back-of-the-enve-

lope calcula tion of the time. Mu ltiplyi ng 1,000 records

by 80 columns per record by about 50 lines of BASIC

code per colu mn and th en in clud ing the rate of 100

BASIC instruc tions per second showed that it would

take about ten hours. Had I known that before I built

the program, I wou ld ha "~,, used a faster l anguage . In-

•~,ad, I had an existing system and no choice b ut to

tune the code (using technique s like those described in

the February column) to make it faster. By spending

forty hours of my time replacing 70 lines of BASIC with

110 lines of BASIC an d 30 lines of assemb ly code, I was

able to reduce the time of that phase from 14 hours to

two hours and 20 minutes. That was good enough for

the system, but more than it could have been had I

done an accurate calculation beforehand and then cho-

sen a more appropriate imp lementa tion language.

P r i n c i p l e s

When you use back-of-the-envelope calculations, be

sure to recall Einstei n's famous advice

E v e r y t h i n g s h o u l d b e m a d e a s s i m p l e a s p o s s ib l e , b u t n o

s i m p l e r .

We know that simple calculations aren't too simple by

incl udin g safety factors to compensate for our mistakes

in estimating parameters and our ignorance of the prob-

lem at hand.

P r o b l e m s

1. At what distances can a courier on a bicycle with a

reel of magnetic tape be a more rapid carrier of in-

formation than a 56-kilobaud telephone line? Than a

1200-baud line?

2. If you punc hed the c ontents of a disk onto cards,

would they fit in your office?

3. When is it cost effective to supply a progr ammer

with a computer term inal at home?

4. Suppose the world is slowed dow n by a factor of a

million. How long does it take for your compute r to

execute an instru ction? Your disk to rotate once?

Your disk arm to seek across the disk? You to type

your name?

5. Which has the most computational oomph: a second

of supercomputer time, a minu te of midicomputer

time, an hour of microcomputer time or a day of

BASIC on a personal computer?

6 .

7.

Suppose that a system must make 100 disk accesses

to process a transaction (although some systems

need fewer, some require man y hundre ds per trans-

action). How man y trans actions per h our (per disk)

can such a system handle?

A programmer spends one hour of CPU time and one

day of his time to speed up a program by ten percent

on a machine that costs one hund red dollars per

hour of CPU time. Individual runs of the program

typically require a minute of CPU time; how long

will it take to pay for the speedu p if the program is

run a h undr ed times a day? What if the speedup

were a factor of two?

S o l u t i o n s t o t h e P r o b l e m s

These solutions include guesses at constants that may

be off by a factor of two, but no t muc h further.

1. 6250 bytes per inc h times a 2400 foot reel of tape

times 0.5 for waste due to blocking gives 90 mega-

bytes per reel of tape. 7000 bytes per second gives 25

megabytes per hour for the line. The bicyclist there-

fore has about three hours to transfer the data,

which gives (say) a 20-mile radius of superiority. The

bicyclist has 50 times as long, or almost a week, to

beat a 1200-baud line.

2. A disk has abou t 200 megabyt es (compa red to 200

kilobytes for a 5.25" floppy disk). A box of punched

cards has 2000 times 80 bytes, or 160 kilobytes, so a

disk is about 1250 boxes of pun che d cards. Tha t 10'

by 6' by 3' pile might fit in my office, but I wouldn't

want it to.

3. A termina l costs a couple of thou sand dollars. A

programmer (including many expenses beyond sal-

ary, unfortuna tely) costs about $100,000/yr, $2000/

wk, $400/day, or $50/hr. Thus once a programmer

has used the terminal for 40 hours, the investment

cost is recovered and the employer gets free work.

(I am told that this scheme was invented by manage-

ment and does not go unnoticed by programmers'

spouses.)

4. A one microseco nd instruc tion takes one second, a

16 msec disk rotation (at 3600 rpm) takes 5 hours, a

30 msec seek takes 10 hours, and the two seconds to

type my name takes about a month.

5. In a second, a supe rcom pute r can do a hundr ed mil-

lion 64-bit floating point operations, a mi dico mpute r

can do one million 16-bit integer additions, a micro-

computer can execute half a million 8-bit instruc-

tions, and BASIC on a personal compu ter ca n exe-

cute one hun dred instructions. The times stated in

the problem work out to about the same amount of

power for the first three machines, while poor

BASIC is left way behind.

6. Ignoring slowdow n due to queuei ng, 30 msec per

disk operation gives 3 seconds per transaction or

1200 transact ions per hour.

182

C o m m u n i c a ti o n s o f t h e A C M M a r c h 1 9 84 V o l u m e 2 7 N u m b e r 3

Page 4: Back of Envelope

8/19/2019 Back of Envelope

http://slidepdf.com/reader/full/back-of-envelope 4/5

7 . T h e c o s t o f t h e c h a n g e i s $ 1 0 0 i n m a c h i n e t i m e p l u s

$ 4 00 i n p r o g r a m m e r t i m e . T h e s a v i n g s o f 1 0

r a i n / d a y i s $ 1 6 / d a y , w h i c h t a k e s a m o n t h t o p a y f or

t h e i n v e s t m e n t . I f t h e s p e e d u p w e r e a f a c to r o f t w o ,

t h e s a v i n g s o f $ 8 0 / d a y w o u l d p a y f o r t h e s p e e d u p i n

a w e e k .

S o l u t i o n s t o F e b r u a r y s P r o b l e m s

1 . O n a m a c h i n e w i t h e i g h t - b i t b y t e s a 2 5 6 -b y t e t a b le

c o u l d r e p r e s e n t t h e c h a r a c t e r s i n e i g h t (p o s s i bl y

o v e r l a p p i n g ] c h a r a c t e r c l a s s e s . T h e I t h b i t i n b y t e J

t e l ls w h e t h e r c h a r a c t e r J i s in c l a s s I . T e s t i n g m e m -

b e r s h i p i n v o l v e s a c c e s s in g a n a r r a y e l e m e n t , A N D -

i n g w i t h a b it p a t t e r n c o n t a i n i n g a s i n g l e o n e , a n d

c o m p a r i n g t h e r e s u l t t o z er o .

Z . G i v e n N a p o w e r o f t w o , w e a r e t o i n i t i a l i z e

C [ 0 . . N - 1 ] s u c h t h a t

C[1]

i s t h e n u m b e r o f o n e b i t s

i n t h e b i n a r y r e p r e s e n t a t i o n o f I . W e u s e t h e i d e n t i t y

t h a t J < 2 K i m p l i e s

C U +

2 K] =

CU] +

1 ; tha t i s ,

t u r n i n g o n t h e K h b i t a d d s o n e t o t h e c o u n t . F o r t h i s

r e a s o n e a c h e l e m e n t i n t h e r i g h t c o l u m n i s p r e c i s e l y

o n e g r e a t e r t h a n t h e c o r r e s p o n d i n g e l e m e n t i n t h e

l e f t c o l u m n :

c [ o ] = o c [ 4 ] =I

c [ i ] =i c [ 5 ] = 2

c [ 2 ] = i c [ 6 ] = 2

c [ 3 ] = 2 c [ v ] = 3

T h e c o d e

t a b l e a n d

i n c r e m e n t i n g .

c [ ] :=

M := I

w h i l e M < N d o

/ * i n v a r i a n t : C [ O . . M - I ] i s

c o r r e c t * /

f o r J : = O t o M - I d o

C [ M + J ] : = C [ J ] + 1

M =

M + M

3 . I f t h e b i n a r y s e a r c h a l g o r i t h m s r e p o r t t h a t t h e y

f o u n d t h e s e a r c h v a l u e T , t h e n i t i s i n f a c t i n t h e

t a b l e. W h e n a p p l i e d t o u n s o r t e d t a b l es , t h o u g h , t h e

a l g o r i t h m s m a y s o m e t i m e s r e p o r t t h a t T is n ot p r e s -

e n t w h e n i t i n f a c t i s . I n s u c h c a s e s t h e a l g o r i t h m s

l o c a t e a p a i r o f a d j a c e n t e l e m e n t s t h a t w o u l d e s t a b -

l i s h t h a t T i s n o t i n t h e t a b l e w e r e i t s o r t e d .

4 . B r o o k s c o m b i n e d t w o r e p r e s e n t a t i o n s f o r t h e t a b l e.

T h e f u n c t i o n g o t t o w i t h i n a f e w u n i t s o f t h e t r u e

a n s w e r , a n d t h e s i n g le d e c i m a l d i g i t s t o r e d i n t h e

a r r a y g a v e t h e d i f f e r e n c e .

5 . A l l t h e d a t a s t r u c t u r e s m e n t i o n e d c a n e m p l o y a n

a d d i t i o n a l s e n t i n e l n o d e t o r e m o v e a t e s t fr o m t h e

i n n e r l o o p o f t h e s e a r c h . B e f o r e a s e a r c h s t a r t s i t

p l a c e s t h e v a l u e b e i n g s e a r c h e d f o r i n t h e s e n t i n e l

n o d e , w h i c h g u a r a n t e e s t h a t t h e s e a r c h w i l l f i n d it s

t a r ge t . W h e n t h e s e a r c h s u c c e e d s a s in g l e c o m p a r i -

s o n te l l s w h e t h e r i t f o u n d a r e a l v a l u e o r t h e o n e

t h e r e f o r e s t a r ts w i t h a s i n g l e - e l e m e n t

r e p e a t e d l y d o u b l e s i t s s i ze b y c o p y i n g a n d

Programm ing Pearls

i n t h e s e n t i n e l. T h i s r e m o v e s f r o m t h e i n n e r l o o p a

t e s t t o d e t e r m i n e w h e t h e r t h e d a t a s t r u c t u r e i s y e t

e x h a u s t e d .

L i n k e d l i s t s h a v e s e n t i n e l n o d e s a t t h e v e r y e n d ;

t h e y m u s t a l s o s t o r e a p o i n t e r t o t h a t n o d e ( t h i s i s

p a r t i c u l a r l y c o n v e n i e n t i n a c i r c u l a r l y l i n k e d l i st

w i t h a d u m m y n o d e ). C l o s e d h a s h t a b l e s u s e a

s e n t i n e l c e l l a t t h e e n d o f t h e a r r a y . N i l p o i n t e r s i n a

s t a n d a r d b i n a r y s e a r c h t r e e a r e r e p l a c e d b y p o i n t e r s

t o a s i n g l e s e n t i n e l n o d e i n t h e m o d i f i e d t r e e .

U p d a t e s o n O l d C o l u m n s

T h e A u g u s t c o l u m n d e s c r i b e d a s o r t i n g p r o g r a m t h a t

u s e d t h e i n t e g e r s t o b e s o r t e d a s i n d i c e s i n t o a b i t m a p .

A u g u s t u s T . C r o c k e r , J r., o f S h a r e d M e d i c a l S y s t e m s

C o r p o r a t i o n p o i n t s o u t t h a t t h e k e y - i n d e x i n g t e c h -

n i q u e c a n a l s o b e u s e d t o a c c e s s t h e I th r e c o r d i n a d i s k

f il e . E v e n t h o u g h d i r e c t a c c e s s o n d i s k w a s n o t a p p r o -

p r i a t e f o r th e p a r t i c u l a r s o r t i n g p r o b l e m ( a q u i c k c a l c u -

l a t i o n s h o w s t h a t 2 7 ,0 0 0 d i s k r e a d s a n d a s m a n y w r i t e s

w o u l d t a k e a t l e a s t 1 0 m i n u t e s , w h e r e a s t h e s o l u t i o n i n

t h e c o l u m n t o o k a fe w s e c o n d s) , t h e t e c h n i q u e i s q u i t e

u s e f u l i n m a n y a p p l i c a t io n s .

T e s t i n g t h e s o r t r o u t i n e r e q u i r e d s h u f f l i n g a f i l e o f

n u m b e r s ; I m e n t i o n e d i n t h e S e p t e m b e r s o l u t i o n to

P r o b l e m 3 t h a t o n e c a n s h u f f l e a f i l e b y c a l l i n g a s y s t e m

s o rt . A s h l e y S h e p h e r d a n d A l e x W o r o n o w o f th e U n i -

v e r s i t y o f H o u s t o n p o i n t o u t t h a t t h e r e a r e m o r e e f f i-

c i e n t s h u f fl i n g a l g o r i t h m s w h e n t h e d a t a i s s to r e d i n a n

a r r a y i n p r i m a r y m e m o r y . F o r e x a m p l e , K n u t h g i v e s

t h e f o l l o w i n g s h u ff l i n g a l g o r i t h m i n

S e m i n u m e r i c a l A l g o -

r i t h m s :

f o r I : = N d o w n t o 2 d o

s w a p X [ I ] , X [ R a n d I n t 1 , I ) ] )

T h e f u n c t i o n

R a n d l n t 1 , I )

r e t u r n s a n i n t e g e r c h o s e n

u n i f o r m l y f r o m t h e r a n g e 1 . . 1 .

T h e S e p t e m b e r c o l u m n d e s c r i b e d E d R e i n g o l d 's p r o b -

l e m o f f i n d i n g a 2 0 - b i t i n t e g e r n o t o n a t a p e c o n t a i n i n g

o n e m i l l i o n s u c h i n t e g e rs , a n d h i s s o l u t i o n b a s e d o n

b i n a r y s e a r c h . D a v i d M a l o n o f t h e U n i v e r s i t y o f K e n -

t u c k y o b s e r v e d t h a t t h e s i m p l e r s o l u t i o n o f l o o k i n g to

s e e w h e t h e r a n y o f t h e f i r st 5 12 i n t e g e r s i s m i s s i n g

( u s i n g t h e 5 1 2 b i t s i n 3 2 1 6 - b i t w o r d s ) f a i l s t o

f i n d a m i s s i n g i n t e g e r w i t h p r o b a b i l i t y o f o n l y

0 . 0 00 0 0 0 0 00 0 0 3 , a s s u m i n g t h a t a l l d i s t i n c t s u b s e t s o f

i n t e g e r s a r e e q u a l l y l i k e l y to a p p e a r o n t h e i n p u t t a p e.

H i s id e a l e a v e s o p e n t h e p r o b l e m o f r e l a x i n g t h a t a s -

s u m p t i o n b y u s i n g a r a n d o m i z a t i o n a l g o r i t h m . E g g e r t

E h r a k e o f W e s t B e r l i n p o i n t s o u t t h a t t h e b i n a r y r a d i x

s e a r c h o f R e i n g o l d ' s s o l u t i o n s u g g es t s a b i n a r y l e x i c o -

g r a p h i c s o r t t h a t u s e s t h r e e w o r k t a p e s a s i t s q u e u e s .

J a m e s W o o d s o f I n f o r r a a ti c s G e n e r a l C o r p o r a t i o n g e n -

e r a l i z e d th e a n a g r a m s p r o b l e m o f t h e q e p t e m b e r c o l -

u m n t o t h e p r o b l e m o f c o m p u t i n g m u l n w o r d a n a g r a m s

i n a l o v e l y p a p e r e n t i t l e d O n C o m p u t i n g A n a g r a m s .

T h a t t i t le i s i t s e lf a r a u l t i w o r d a n a g r a m o f s u c h c o m -

m o n p h r a s e s a s r o m a n t i c p a n g a m o n g u s , m a n g o s m o a n

c a p t u r i n g , a n d g a u n t m o r o n c a m p a i g n s , a m o n g o t h e r s n o t

M a rc h 1 9 8 4 Vo l u m e 2 7 N u mb e r 3 C o mm u n i ca t io n s o f t h e AC M 1 83

Page 5: Back of Envelope

8/19/2019 Back of Envelope

http://slidepdf.com/reader/full/back-of-envelope 5/5

Programming Pearls

Further Reading

I haven't been able to find much written about back-of-

the-envelope calculations --the idea is usually passed

on by example. Similar ideas used to be taught as stu-

dents learned how to use slide rules; they had to add

exponents in their heads and to check all answers for

reasonableness. The best written discussion I have seen

on such common sense ideas in mathematics is Barry

Cipra's c harming booklet

M I S T E A K S . . . a n d h o w t o fi n d

t h e m b e fo r e t h e t e a c h e r d o e s . . . . This 70-page gem is a

supplement for calculus courses; it was published in

1983 by Birkh~iuser Boston.

Denning and Buzen describe a formal but simple

method for estimating critical parameters in computer

systems performance. Their paper "Operational Analy-

sis of Queueing Network Models" appears in C o m p u t i n g

S u r v e y s 1 0 ,

3, November 1978, pp. 225-261.

suitable for print. Do readers know of more work on the

fascinating combinatorial puzzle of multiword ana-

grams?

Solution 7 in the October column described how Jeff

Ullman's problem of determining wheth er any K-ele-

ment subset of an N-element set sums to at most T can

be solved by algorithms with running time of

O N

log N) and

O N ) .

Wayne Amsbury of Maryville.

Missouri, gave two algorithms with runn ing time

O N

log K). When Ullman assigned this as a class exer-

cise, students designed algorithms with all the running

times mentioned above, as well as

O N K ) ,

O(N2), and

O N K ) .

Can you find natural algorithms to go with those

running times?

F o r C o r r e s p o n d e n c e : ] o n L . B e n t l e y . A T & T B e l l L a b o r a t o r i e s . R o o m 2 C -

3 1 7 . 6 0 0 M o u n t a i n A v e n u e . M u r r a y H i ll . N J 0 7 9 7 4 .

P e r m i s s i o n t o c o p y w i t h o u t f e e a ll o r p a r t o f t h i s m a t e r i a l i s g r a n t e d

p r o v i d e d t h a t t h e c o p i e s a r e n o t m a d e o r d i s t r i b u t e d f o r d i r e c t c o m m e r -

c i a l a d v a n t a g e , t h e A C M c o p y r i g h t n o t i c e a n d t h e t i tl e o f t h e p u b l i c a t i o n

a n d i t s d a t e a p p e a r , a n d n o t i c e i s g i v e n t h a t c o p y i n g i s b y p e r m i s s i o n o f

t h e A s s o c i a t i o n f or C o m p u t i n g M a c h i n e r y . T o c o p y o t h e r w i s e , o r t o

r e p u b l i s h , r e q u i r e s a f e e a n d / o r s p e c i fi c p e r m i s s i o n .

In R ece nt S IG Publ ica t ion s

SlG CT (Automata and Computability Theory):

SIGACT News, Vol. 15, No. 2, Spring/Summer 1983,

80 pages; Vol. 15, No. 3, Summer/Fall 1983, 96 pages

(quarterly)

SlGAPL (APL Programming Language): APL Quote

Quad, Vol. 13, No. 4, June 1983, 44 pages (quarterly)

SlGARCH (Architecture of Computer Systems): Com-

puter Archi tectu re News, Vol. 11, No. 4, September

1983, 56 pages (quarterly)

SIGART (Artificial Intelligence): SIGART Newsle tter

#85, July 1983, 44 pages; #86, October 1983, 72 pages

(quarterly)

SIGBDP

(Business Data Processing and Management):

Database, Vol. 14, No. 4, Summer 1983, 52 pages; Vol.

15, No. 1, Fall 1983, 44 pages (quarterly)

$1GCAPH Computers and the Physical ly Handicapped):

SIGCAPH Newsletter, #32, Spring 1983, 24 pages

(quarterly)

SlGCAS (Computers and Society): Compute rs and So-

ciety Newsletter, Vol. 13, No. 3, Summer 1983, 24

pages (quarterly)

SlGCHI (Computer and Hum an Interaction): SIGCHI

Bulletin, Vol. 15, Nos. 2 & 3, October 1983, 32 pages

(quarterly)

SlGCOMM (Data Com munica tion): SIGCOMM Com-

puter Communication Review, Vol. 13, No. 3, July

1983, 60 pages; Vol. 13, No. 4, October 1983, 272 pages,

Proceedings of the 8th Data Communications Sym-

posium (quarterly)

SlGCPR (Computer Personnel Research): SIGCPR

Newsletter, Special Proceedings of the 20th Annual

CPR Conference, VA, 148 pages (quarterly)

SlGCSE(Computer Science Education): SIGCSE Bulle-

tin, Vol. 15, No. 3, September 1983, 52 pages; Vol. 15,

No. 4, December 1983, 32 pages (quarterly)

SlGCUE (Computer Uses in Education): SIGCUE Bulle-

tin, Vol. 17, No. 3, Summer 1983, 36 pages; Vol. 17,

No. 4, Fall 1983, 36 pages (quarterly)

SlGDA (Design Automation) : SIGDA Newsletter, Vol.

13, No. 2, July 1983, 64 pages; Vol 13, No. 3, December

1983, 44 pages (quarterly)

SlGDOC (Documenta tion): Asterisk , Vol . 9, No. 3, Oc-

tober 1983, 20 pages (Bi-monthly)

SlGGRAPH (Computer Graphics): Computer Graphics ,

Vol. 17, No. 4, October 1983, 88 pages, (quarterly)

SlGMICRO (Microprogramming): SIGMICRO Newslet-

ter, Vol. 14, No. 3, September 1983, 32 pages; Vol. 14,

No. 4, December 1983, 248 pages, Proceedings of the

16th Annual Microprogramming Workshop (quart-

erly)

SlGNUM (Numerica l Mathematics): SIGNUM Newslet-

ter, Vol. 18, No. 4, October 1983, 24 pages (quarterly)

SlGOA (Office Automation): SIGOA Newsletter, Vol. 4,

No. 1, Summer 1983, 20 pages (quarterly)

SlGOPS (Operating Systems): Oper ating Systems Re-

view, Vol. 17, No. 4, October 1983, 16 pages; Vol. 17,

No. 5, Special Issue, 164 pages, Proceedings of the 9th

Annual Symposium on Operating Systems Review

(quarterly)

SlGPC (Personal Computing): SIGPC Notes, Vol. 5, Nos.

3 & 4, Spr ing/Sum mer 1983, 24 pages, Vo l. 6, No. 1,

Fall 1983, 20 pages (quarterly)

84 C o m m u n ic a ti o ns o f t h e A C M M a r c h 1 9 8 4 V o l um e 2 7 N u m b e r 3