From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1d321b3a6b8bcab2 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-01-19 20:43:52 PST Path: nntp.gmd.de!newsserver.jvnc.net!nntpserver.pppl.gov!princeton!udel!news.mathworks.com!hookup!swrinde!news.uh.edu!uuneo.neosoft.com!Starbase.NeoSoft.COM!not-for-mail From: smize@Starbase.NeoSoft.COM (Samuel Mize) Newsgroups: comp.lang.ada Subject: Re: "Subtract C, add Ada" Date: 19 Jan 1995 22:43:52 -0600 Organization: NeoSoft Internet Services +1 713 684 5969 Message-ID: <3fnf28$s3f@Starbase.NeoSoft.COM> References: <3etund$hnr@miranda.gmrc.gecm.com> <3f4mbe$rud@cronkite.seas.gwu.edu> <3f5s92$3id@info.epfl.ch> NNTP-Posting-Host: starbase.neosoft.com Date: 1995-01-19T22:43:52-06:00 List-Id: In article <3f5s92$3id@info.epfl.ch>, Laurent Gasser wrote: > >The problem is in the language definition, not in the choic of symbols. >C is one of the rare language I use to allow having an assignment after >the if statement. All other languages only allow for a boolean test. > >I never saw a good reason for allowing this. > It's a question of who optimizes your code. In the 60s, when C was developed, compilers *couldn't* optimize code, so the programmer had to. Many apparently-bizarre C capabilities are there to support optimization level -1 (programmer does the optimizations). Consider the common C idiom to copy a null-terminated array, usually a string [syntax may not be quite right]: while (*a++=*b++); Recall that C considers 0 false, a null character is integer 0, and an assignment returns what was assigned. This loop condition copies the character at address b to address a, increments both, and exits when a null was copied. There is no statement inside the loop. This is more efficient, with a non-optimizing compiler, than while (*a) { *a = *b; a=a+1; b=b+1}; /*or whatever is right syntax*/ because * The operator ++ increments the value of while it's still in a register. The second code reads a from memory to do the character assign, reads it again to increment it. * ++ can use the assembler's increment instruction. a=a+1 could not. * Using the assignment as the loop expression indirectly directs the compiler to keep the result of the assignment in a register for reuse in the expression, instead of writing it to memory, then reading it. Thus, with a lot of man-hours, expertise and skull-sweat, someone using a basic C compiler can come within an order of magnitude of the efficiency of someone using a half-good optimizing Ada compiler. Often. Of course, if you only *read* the code, and don't *run* it, it looks like the C is more optimized, because you can *see* the optimizations. With Ada you write less-optimized-looking code, and trust the compiler. (But verify.) >-- >Laurent Gasser (gasser@dma.epfl.ch) >Computers do not solve problems, they execute solutions. > >I know very few ideas worth dying for, none is worth killing. I dunno, I've killed quite a few ideas in my time :-) Sam Mize - smize@starbase.neosoft.com