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.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,2a34b7ad6c6a0774 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!q22g2000yqm.googlegroups.com!not-for-mail From: Gene Newsgroups: comp.lang.ada Subject: Re: Efficiency of code generated by Ada compilers Date: Sun, 8 Aug 2010 07:03:43 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: 184.12.86.20 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1281276223 29172 127.0.0.1 (8 Aug 2010 14:03:43 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 8 Aug 2010 14:03:43 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: q22g2000yqm.googlegroups.com; posting-host=184.12.86.20; posting-account=-BkjswoAAACC3NU8b6V8c50JQ2JBOs04 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-us) AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8,gzip(gfe) Xref: g2news1.google.com comp.lang.ada:12954 Date: 2010-08-08T07:03:43-07:00 List-Id: On Aug 6, 4:21=A0pm, Elias Salom=E3o Helou Neto wrote: > I would like to know how does code generated by Ada compilers compare > to those generated by C++. I use C++ for numerical software > implementation, but I am trying to find alternatives. One thing, > however, I cannot trade for convenience is efficiency. Will Ada > compiled code possibly be as efficient as that generated by C++ > compilers? > > Also, I do need to have something similar to C++ "templated > metaprogramming" techniques. In particular, C++0x will introduce > variadic templates, which will allow us to write templates that will > generate efficient, type-safe, variable-argument functions. Is there > anything like that in Ada? > > If any of the above questions is to be negatively answered, I ask: why > does Ada even exist? And further, is there any language which is > _truly_ better (regarding code maintainability, readability and > developing ease) than C++ and as overhead-free as it? My experience is that GNAT produces code very similar to GCC-compiled C ++ for equivalent programs. In some cases, a built-in Ada construct will generate better code than a "hand-coded" equivalent in C++. An example I ran into recently was incrementing a modular type. In Ada, you say I :=3D I + 1;, and the compiler takes care of "wrapping" to zero. In C++, I've frequently seen people write i =3D (i + 1) % n; to simulate the same operation in the absence of modular types. In this case, GCC/C++ generates a div/ mod instruction, which is very expensive. GNAT generates if (n =3D=3D const(n - 1)) i =3D 0; else i++; In the past, I have seen examples where Ada array indexing was much more efficient than C++, apparently because GCC could not make needed inferences about aliasing. In some cases, GNAT "compiles in" error checking that you must turn off to get the equivalent GCC code. There's no down side in this, as it means you never had to write the error checking (overflows, array bounds, etc.), which is much more expensive and error-prone than letting GNAT do it for you (and then possibly turning selected bits off in the 1% of cases where it makes a difference). And of course it's even more expensive never to check for errors at all, adding risk of an undiscovered bug in production, which an unfortunate amount of C+ + code does. Finally, there are a few places where GCC/C++ does produce somewhat better code than GNAT simply because there are more people tweaking the C++ portion of GCC. I can't recall ever seeing the reverse occur. So it goes. There will always be differences among compilers. Design and idiomatic coding in Ada wouldn't benefit from variadic functions and procedures. Various combinations of overloading, named parameters with default values, and aggregates accomplish the same objectives in a more coherent manner. Certainly efficient variadic functions would be useful in C++ because they're specified in the libraries. You were asking reasonable questions until the last paragraph. So I'll stop here. But see comp.lang.c where C zealots frequently make similar assertions about C with respect to C++.