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.7 required=5.0 tests=BAYES_00,INVALID_DATE, MSGID_SHORT,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!ucsd!ucbvax!avocado.UUCP!garym From: garym@avocado.UUCP (Gary Morris) Newsgroups: comp.lang.ada Subject: Re: how to do this in Ada? Message-ID: <256@avocado.uucp> Date: 18 Mar 90 03:23:54 GMT Sender: usenet@ucbvax.BERKELEY.EDU Reply-To: garym@sugar.hackercorp.com Organization: The Internet List-Id: In article <51041@cc.utah.edu>, dharvey@wsccs.weber.edu writes: > In article <1771@awdprime.UUCP> sanders@sanders.austin.ibm.com (Tony Sanders) writes: >>How do you do this in ADA? >> >> switch(n) { >> case 0: >> count++; >> case 1: >> ocount++; >> case 2: >> printf("%d %d\n",count,ocount); >> break; >> default: >> printf("unknown n\n"); >> break; >> } >> >Here is how you would really do it. > >if n = 0 then > count := count + 1; >end if; >if n = 0 or n = 1 then > ocount := ocount + 1; >end if; >if n = 0 or n = 1 or n = 2 then > write... -- the equivalent here takes a few lines of code >else > write... -- ditto for this one >end if; > Here is how I would do it: if n in 0..2 then if n in 0..1 then if n = 0 then count := count + 1; end if; ocount := ocount + 1; end if; Int_IO.Put(count); Text_IO.Put(' '); Int_IO.Put(ocount); Text_IO.New_Line; else Text_IO.Put_Line("unknown n"); end if; If you're really concerned about the efficiency of the Ada code, let's take a closer look at what comes out. This last version is probably the most efficient way to code this in Ada. A total of 4 compares would be generated (2 for the "n in 0..2", 1 for the "n in 0..1"). In the previous Ada version there are 6 compares and all of them always executed (except the "or" might be short circuited). The C compiler will probably generate a table jump. But before it can index into the table it must make sure that the index is within the range 0..2, that takes 2 compares. Then we have to index into the table (a shift/load or equivalent) and branch. Ada C compares branches compares branches shift/ n executed taken total executed taken loads total -- -------- -------- ----- -------- -------- ------ ----- -1 1 1 2 1 1 0 2 0 4 1 5 2 2 2 6 1 4 2 6 2 2 2 6 2 3 2 5 2 2 2 6 3 2 1 3 2 1 0 3 4.2 4.6 We have 5 possibilities. Assuming they are of equal probability (a poor assumption, but we have no data, so let's use it for the sake of discussion), the average number of instructions is 4.2 for Ada and 4.6 for C. Of course, all instructions don't take the same amount of time to execute, branches taken are especially bad since they break the pipeline, and if the table jump code really did a shift (on a VAX) that would be awful (VAX shifts take as much time as about 4 add instructions) but it probably comes out as some indexed addressing mode. There are a number of other complications in determining which is fastest. I used VAX for the above example since I recently wrote an optimizing code generator for an Ada compiler targetted to the VAX. Targetting to other processors will give you different results. What is the distribution of the values on n? If it is usually going to be 0, for example, then we might code differently to take advantage of that. Additionally, this entire analysis must be redone if you change the number of case entries in the switch statement. In short we can't determine from comparing C and Ada source which is faster unless we know what kind of optimizations each compiler will do and what the instruction set of the target machine is like. In comparing these two languages what we have here is a tradeoff, we could either have the flexibility of the C switch statement where it is easy to add code to one case and not realize that another case may fall into it OR less flexibility in Ada but with less chance of introducing problems during future maintenance and enhancement. Since one of the goals of Ada was to reduce the life cycle costs of software, the bulk of which is spent in the maintenance phase, it makes sense that the Ada designers chose this form for this control structure. Finally, though, which one is fastest? It all depends on whether printf interpreting a control string is faster than the calls to Text_IO. The time spent in these control structures is totally insignificant compared to the time spent doing a subprogram call to printf or text_io. --GaryM -- Gary Morris Internet: garym@sugar.hackercorp.com Lockheed, Houston, Texas UUCP: uunet!sugar!garym Space Station Freedom Project Phone: +1-713-283-5195