p896 Bentley

download p896 Bentley

of 6

Transcript of p896 Bentley

  • 8/3/2019 p896 Bentley

    1/6

    programminpearlsBUMPER-STICKER COMPUTER SCIENCEEvery now and then, programmers have to convertunits of time. If a program processes 100 records persecond, for instance, how long will it take to processone million records? Dividing shows that the task takes10,000 seconds, and there are 3600 seconds per hour, sothe answer is about three hours.But how many seconds are there in a year? If I tellyou there are 3.155 X 107, you wont even try to re-member it. On the other hand, who could forget that, towithin half a percent,

    rule is usually the person who sent me the rule, even ifthey in fact attributed it to their Cousin Ralph (sorry,Ralph). In a few cases 1 have listed an earlier reference,together with the authors current affiliation (to thebest of my knowledge]. Im sure that 1 have slightedmany people by denying them proper attribution, andto them I offer the condolence thatPlagiarism is the sincerest form of flattery.

    Anon.?r seconds is a nanocentury.TomDuff

    Bell Labs

    Without further ado, heres the advice, grouped intoa few major categories.

    So if your program takes lo7 seconds, be prepared towait four months.Coding

    Februarys column solicited bumper-sticker-sized ad-vice on computing. Some of the contributions arentdebatable: Duffs rule is a memorable statement of ahandy constant. This rule about a program testingmethod (regression tests save old inputs and outputs tomake sure the new outputs are the same) contains anumber that isnt as ironclad.

    When in doubt, use brute force.Ken ThompsonBell Labs

    Avoid arc-sine and arc-cosine functions-you can usu-ally do better by applying a trig identity or computing avector dot-product.Jim ConynghamArvin/Cnlspan Advanced Technology Center

    Regress ion testing cuts test intervals in half.Larry BernsteinBell Communications Research

    Bernsteins point remains whether the constant is 30 or70 percent: these tests save development time.Theres a problem with advice that is even less quan-titative. Everyone agrees thatAbsence makes the heart grow fonder.

    Anorl.

    Allocate four digits for the year part of a date: a newmillenium is coming.David MartinNorristown, Petmsylvania

    Avoid asymmetry.Andy HuberData General Corporation

    andOut of sight, out of mind.

    The sooner you start to code, the longer the programwill take.Roy CarlsonAnon. University of Wisconsin

    Everyone, that is, except the sayings themselves-theyare contradictory . There are similar contradictions inthe slogans in this column. Although there is sometruth in each saying in this column, all should be takenwith a grain of salt.

    If you cant write it down in English, you cant code itPeter HalpernBrooklyn, New York

    A word about credit. The name associated with aQ1985 ACMOOOl-0782/85/0900-0896 750

    Details count.Peter WrinbergerBell Labs

    a96 Communications of the ACM September 1985 Volume 28 Number 9

  • 8/3/2019 p896 Bentley

    2/6

    Programming Pearls

    If the code and the comments disagree, then both areprobably wrong.Norm Sch yerBelt LabsA procedure should fit on a page.David TribbleArlington, TexasIf you have too many special cases, you are doing itwrong. Craig Zerouni

    Computer FX Ltd.London, England

    Get your data structures correct first, and the rest ofthe program will write itself.David IonesAssert, The Netherlands

    User Interfaces[The Principle of Least Astonishment] Make a user in-terface as consistent and as predictable as possible.Contributed by several readersA program designed for inputs from people is usuallystressed beyond the breaking point by computer-generated inputs.

    Dennis RitchieBell Labs

    It takes three times the effort to find and fix bugs insystem test than when done by the developer. It takesten times the effort to find and fix bugs in the field thanwhen done in system test. Therefore, insist on unit testsby the developer.Larry BernsteinBell Commun ications Research

    Dont debug standing up. It cuts your patience in half,and you need all you can muster.Dave StorerCedar Rapids, IowaDont get suckered in by the comments-they can beterribly misleading. Debug only the code.Dave Storer

    Cedar Rapids, IowaTesting can show the presence of bugs, but not theirabsence.Edsger W. Dijkstra

    University of TexasEach new user of a new system uncovers a new class ofbugs.

    Brian KernighanBell LabsIf it aint broke, dont fix it.

    Ronald ReaganSanta Barbara, California

    Twenty percent of all input forms filled out by peoplecontain bad data. [The Maintainers Motto] If we cant fix it, it aintbroke.Vie Vyssotsky Lieutenant Colonel Walt WeirBell Labs United States ArmyEighty percent of all input forms ask questions theyhave no business asking.Mike Garey

    Bell Labs

    The first step in fixing a broken program is getting it tofail repeatably.Tom DuffBell LabsDont make the user provide information that the sys-tem already knows.Rick Lemons

    Cardinal Data SystemsFor 80 percent of all data sets, 95 percent of the infor-mation can be seen in a good graph.William S. Cleveland

    Bell Labs

    DebuggingOf all my programming bugs, 80 percent are syntaxerrors. Of the remaining 20 percent, 80 percent are triv-ial logical errors. Of the remaining 4 percent, 80 per-cent are pointer errors. And the remaining 0.8 percentare hard.Marc DonnerIBM T. 1. Watson Research Center

    Performance[The First Rule of Program Optimization] Dont do it.[The Second Rule of Program Optimization-For ex-perts only] Dont do it yet.Michael jacksonMichael lackson Systems Ltd.The fastest algorithm can frequently be replaced by onethat is almost as fast and much easier to understand.Douglas W. Iones

    University of lowaOn some machines indirection is slower with displace-ment, so the most-used mem ber of a structure or arecord should be first.Mike MortonBoston, Massachusetts

    September 1985 Volume 28 Number 9 Communica tions of the ACM 097

  • 8/3/2019 p896 Bentley

    3/6

    Programming Pearls

    In non-I/O-bound programs, a few percent of thesource code typically accounts for over half the runtime. Don KnuthStanford UniversityBefore optimizing, use a profiler to locate the hotspots of the program.Mike MortonBoston, Massachusetts[Conservation of Code Size] When you turn an ordinarypage of code into jus t a handful of instructions forspeed, expand the comments to keep the number ofsource lines, constant.Mike MortonBoston, MassachusettsIf the programm er can simulate a construct faster thanthe compiler can implement the construct itself, thenthe compiler writer has blown it badly.Gu:y L. Steele, jr.Tartan LaboratoriesTo speed up an I/O-bound program, begin by account-ing for all 1,/O. Eliminate that which is unnecessary orredundant, and make the remaining as fast as possible.David Martin

    Norristown, Pennsylvania

    The fastest I/O is no I/O.Nils-Peter NelsonBell Labs

    The cheapest, fastest, and most reliable components ofa computer system are those that arent there.Gordon BellEncore Computer Corporation[Compiler Writers Motto-Optimization Pass] Makinga wrong program worse is no sin.Bill McKeemanWang ZnstituteElectricity travels a foot in a nanosecond.Commodore Grace Murray HopperUnited States NavyLISP programmers know the value of everything butthe cost of nothing.Alan Perlis

    Yale University[Littles Formula] The average number of objects in aqueue is the product of the entry rate and the averageholding time.Peter DenningRL4cs

    Documentation[The Test of Negation] Dont include a sentence in doc-umentation if its negation is obviously false.Bob MartinAT&T TechnologiesWhen explaining a command, or language feature, orhardware widget, first describe the problem it is de-signed to solve.

    David MartinNorristown, Pennsylvania[One Page Principle] A (specification, design, proce-dure, test plan) that will not fit on one page of 8.5-by-l.1inch paper cannot be understood.Mark Ardis

    Wang InstituteThe jobs not over until the paperworks done.Anon.

    Managing SoftwareThe structure of a system reflects the structure of theorganization that built it.Richard E. FairleyWang InstituteDont keep doing what doesnt work.Anon.[Rule of Credibility] The first 90 percent of the codeaccounts for the first 90 percent of the developmenttime. The remaining 10 percent of the code accountsfor the other 90 percent of the development time.Tom Cargill

    Belt LabsLess than 10 percent of the code has to do with theostensible purpose of the system; the rest deals withinput-output, data validation, data structure mainte-nance, and other housekeeping.

    May ShawCarnegie-Mellon UniversityGood judgment comes from experience, and experiencecomes from bad judgment.Fred Brooks

    University of North CarolinaDont write a new program if one already does more orless what you want. And if you must write a program,use existing code to do as much of the work as possible.Richard HillHewlett-Packard S.A.Geneva, SwitzerlandWhenever possible, steal code.Tom DuffBell Labs

    898 Communications of the ACM September 1985 Volume 28 Number 9

  • 8/3/2019 p896 Bentley

    4/6

    Programming Pearls

    Good customer relations double productivity.Larry BernsteinBell Communications Research

    Translating a working program to a new language orsystem takes 10 percent of the original developmenttime or manpower or cost.Douglas W. JonesUniversity of Iowa

    Dont use the computer to do things that can be doneefficiently by hand.Richard HillHewlett-Packard S.A.Geneva, Switzerland

    Dont use hands to do things that can be done effi-ciently by the computer.Tom DuffBell LabsId rather write programs to write programs than writeprograms.

    Dick SitesDigital Equipment Corporation[Brookss Law of Prototypes] Plan to throw one away,you will anyhow.Fred Brooks

    University of North CarolinaIf you plan to throw one away, you will throw awaytwo. Craig Zerouni

    Computer FX Ltd.London, England

    Prototyping cuts the work to produce a system by 40percent. Larry BernsteinBell Communications Research[Thompsons rule for first-time telescope makers] It isfaster to make a four-inch mirror then a six-inch mirrorthan to make a six-inch mirror.

    Bill McKeemanWang Institute

    Furious activity is no substitute for understanding.H. H. WilliamsOakland, California

    Always do the hard part first. If the hard part is impos-sible, why waste time on the easy part? Once the hardpart is done, youre home free.Always do the easy part first. What you think at first isthe easy part often turns out to be the hard part. Oncethe easy part is done, you can concentrate all yourefforts on the hard part.

    Al SchapiraBell Labs

    . ...r.il-.!ii)~::,: ii tlr:If you lie to the computer, it will get you.Perry FarrarGermantown, MarylandIf a system doesn t have to be reliable, it can do any-thing else.H. H. WilliamsOakland, CaliforniaOne persons constant is another persons variable.Susan Gerhart

    Microelectronics and Computer Technology Corp.One persons data is another persons program.Guy L. Steele, r.Tartan Laboratories

    If youve made it this far, youll certainly apprec iatethis excellent advice.Eschew clever rules.Joe CondonBell Labs

    Although this column has allocated just a few words toeach rule, mos t could be greatly expanded (say, into anundergraduate paper or into a bull session over a fewbeers). These problems show how one might expandthe following rule.Make it work first before you make it work fast.

    Bruce WhitesideWoodridge, IttinoisYour assignment is to expand other rules in a similarfashion.Restate the rule to be more precise. The examplerule might actually be intended as

    Ignore efficiency concerns until a program is knownto be correct.or asIf a program doesnt work, it doesnt matter howfast it runs; after all, the null program gives a wronganswer in no time at all.Present small, concrete examples to support yourrule. In Chapter 7 of their Elements of ProgrammingStyle, Kernighan and Plauger present 10 tangledlines of code from a programming text; the convo-luted code saves a single comparison (and inciden-tally introduced a minor bug). By wasting anextra comparison, they replace the code with twocrystal-clear lines. With that object lesson fresh onthe page, they present the ruleMake it right before you make it faster.

    September 1985 Volume 28 Number 9 Communications of the ACM

  • 8/3/2019 p896 Bentley

    5/6

    Programming Pearls

    3. Find war stories of how the rule has been used inlarger programs.a. It is pleasant to see the rule save the day. Car-negie-Mellon Computer Science Report CMU-CS-83-108 describes how I made a system rightbefore I made it faster: I built several programsin 2,000 lines of clean code, most of whichworked like a charm. Unfortunately, one 600-line program to be ru:n several times a day re-quired 14.6 hours. Profiling showed that 66lines of code accounted for 13.6 hours of therun time, and just 3 lines accounted for 11hours (10 percent of the code took 94 percent ofthe time, and 0.6 percent of the code took 7.8percent of the time). I therefore concentratedmy efficiency efforts on those hot spots, andproperly ignored efficiency elsewhere.b. It can be even more impressive to hear howignor:ing the rule 1ead.so a disaster. When VieVyssotsky modified a Fortran compiler in the

    function maxheap(l,u, i] {Clifaheapfor (i = 2*1; i = x[c]) breakt=x[ i I ; x[i]=x[cl; x[cl=t t swap i, ci a ctassert(maxbeap(1.u). "siftdown post"))function draw(i,s) {if (i d:= n) {print i ":", 8, x[i]draw( 2ci, 8'" ")draw(2ri+l, s " ")1)$I== "draw" { draw(1. ""1 ]tl=="down" I siftdown(52, $3) ]Sl==asserl: { assert(maxheap(S2, $3), !'cmd") }$l""X" { xtS21=$3 1$la=n { n=$2 }

    PROGRAM1. An AWK Testbed for Heaps

    early 1960s he spent a week making a correctroutine very fast, and thereby introduced a bug.The bug surfaced two years later, because theroutine had never once been called in over100,000 compilations. Vyssotskys week of pre-mature optimization was worse than wasted: itmade a good program bad. [This story, though,served as finetraining for Vyssotsky and gener-ations of Bell Labs programmers.)4. Critique the rules: which are always capital-TTruth and which are sometimes misleading? BillWulf of Tartan Laboratories took only a brief con-versation to convince me that if a program doesntwork, it doesnt matte r how fast it runs isnt quiteas true as I once thought. He used the case of adocument production system that we both used. Al..though it was faster than its predecessor, at times itseem ed excruciatingly slow: it took several hours tocompile a book. Wulfs clincher went like this:That program, like any other large system, todayhas 10 known, but minor, bugs. Next month, it willhave 10 different known bugs. If you could choosebetween removing the 10 current bugs or makingthe program run 10 times faster, which would youpick?

    Solutions for Julys ProblemsSeveral readers reported a horrible typographical erroron page 672 of the July column. In the fourth line of thesecond paragraph in the right-hand column, the assign-ment 1=m+7 should have been l=m+l ; the numberone was mistakenly typeset as a seven. Solutions 1,2, and 3 refer to Program 1.1.

    2.

    Program 1 is a testbed for the sif tdown routine.The recursive draw routine uses indentation toprint the implicit tree structure of the heap (thesecond parameter in the recursive call appends fourspaces to the indentation string s).The modified assert routine in Program 1 in-cludes a string variable that provides informationabout the assertion that failed. Many systems pro-vide an assertion facility that automatically givesthe source file and the line number of the invalidassertion.3. The sif tdown routine in Program 3 uses theassert and maxheap routines to test the pre- andpost-conditions on entry and exit. The maxheaproutine requires 0(&L) time, so the assert callsshould be removed from the production version ofthe code.4. The tests in the last column missed a bug in myfirst s if tup procedure. I mistakenly initialized iwith the incorrect assignment i=n rather than withthe correct assignment i=u. In all my tests, though,u and n were equal, so they did not identify thebug. (The published sif tup was correct, however,as far as I know.)

    5, I commonly use scaffolding to time algorithms.6. For this solution, I gathered data on the run of

    900 Communications of the ACM September1985 Volume 28 Number 9

  • 8/3/2019 p896 Bentley

    6/6

    the Quicksort 2 program described in the April1984 column [with the CutOff parameter set to15). My C program was 92 lines long: 41 lines ofscaffolding supported 51 lines of real code.(The real code took just 26 lines of AWK,and similar scaffolding took 10 lines.) The topgraph plots the run time of Quicksort on aVAX-11/750@ as a function of the size of theinput array, N. The one hundred N values are uni-formly spaced along the logarithmic scale. Thesmall times are discrete because the system mea-sures time in sixtieths of a second. That graph

    Further Reading -If you like heavy dosesof unadorned rules, try TomParkersRuZes of Thumb Houghton Mifflin, 1983).Thefollowing rules appearon its cover798.One ostrich eggwill serve 24 people for brunch.888. A submarine will move through the water mostefficiently if it is 10 to 13 times as ong as t is wide.The book contains 896 similar rules.Strunk and Whites classicElements of Style (h4acmiLIan, third edition 1979) s built around 43 rules such asOmit needlesswords.That rule is elaborated n one and a half pagesof vigor-ous writing, much of which is before-and-afterexam-ples. The book is just 85 pages ong. On a per pagebasis, t is arguably the best style book ever written forEnglish.Kernighan and PlaugersElemenfs of Programm ing Style(McGraw-Hill, secondedition 1978) s similar to StrunkrendWhite both in title and in execution. They illus-trate the ruleKeep t simple to make it faster.on a 2%line sort from a programming text: a straightfor-ward g-line program s 25 percent faster. John ShorecomparesKernighan and Plauger to Strunk and Whitein his SachertorteAlgorithm, And OtherAntidotes To Com-puter Anxiety (Viking, 1985).His side-by-sidepresenta- tion of 10 rules from each shows how good program-ming is similar to good writing. He ends with the ruleDo not take shortcuts at the cost of clarity.and the riddle of whether it is from Strunk and Whiteor from Kernighan and Plauger.Fred BrookssMythical Man Month (Addison-Wesley,1975)contains dozensof rules about software, ncludinghis classic[BrookssLaw] Adding manpower to a late softwareproject makes t later.Butler Lampsons Hints for Computer SystemDesign[IEEESoftware, January 1984) summarize his experiencein building dozensof state-of-the-art systems.

    Programming Pearls

    shows that run time is strongly correlated to in-put size, but provides little insight beyond that.

    IO mscc --j

    8.

    Runtime

    .: . . -.. .**.*:..--. ** : . .: .

    Microsecondsper clcmcnt

    loo-l * II I I II I Iloo 200 500 16602666 5ooo

    N

    The y-scale in the bottom graph is the run time perarray element in microseconds (that is, the totaltime divided by N). This graph displays the widevariation in run time due to the randomizing Swapoperation. The straightness of the data indicatesthat the run time per element grows logarithmic-ally, which implies that the overall run time ofQuicksort is O(N log N). Most algorithms texts givea mathematical proof of this fact.To test that a sort routine permutes its input, wecould copy the input into a separate array, sort thatby a trusted method, and compare the two arraysafter the new routine has finished. An alternativemethod uses only a few bytes of storage, but some-times makes a mistake: it uses the sum of the ele-ments in the array as a signature of those elements.Changing a subset of the elements will change thesum with high probability. (Summing involvesproblems related to word size and nonassociativityof floating-point addition; other signatures, such asexclusive or, avoid these problems.)

    VAX is a trademark of Digital Equipment Corporation.

    For Correspondence: Jon Bentley, AT&T Bell Laboratories, Room Z-317,600 Mountain Ave., Murray Hill, NJ 07974.Permission to copy without fee all or part of this material is grantedprovided that the copies are not made or distributed for direct commer-cial advantage, the ACM copyright notice and the title of the publicationand its date appear. and notice is given that copying is by permission ofthe Association for Computing Machinery. To copy otherwise, or torepublish. requires a fee and/or specific permission.

    September1985 Volume 28 Number 9 Communicationsof he ACh4 901