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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1d575f572a099528 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-12-07 10:53:52 PST Newsgroups: comp.lang.ada Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!out.nntp.be!propagator-SanJose!in.nntp.be!news-out.visi.com!hermes.visi.com!uunet!ash.uu.net!xyzzy!nntp From: Jeffrey Carter Subject: Re: What is faster Ada or C? X-Nntp-Posting-Host: e246420.msc.az.boeing.com Content-Type: text/plain; charset=us-ascii Message-ID: <3C110823.4BC6FF07@boeing.com> Sender: nntp@news.boeing.com (Boeing NNTP News Access) Content-Transfer-Encoding: 7bit Organization: The Boeing Company X-Accept-Language: en References: <3c08314d$0$158$9b622d9e@news.freenet.de> <3C0BA624.7A12BFA1@boeing.com> <3C10C26B.AA451459@nbi.dk> Mime-Version: 1.0 Date: Fri, 7 Dec 2001 18:19:15 GMT X-Mailer: Mozilla 4.73 [en]C-CCK-MCD Boeing Kit (WinNT; U) Xref: archiver1.google.com comp.lang.ada:17604 Date: 2001-12-07T18:19:15+00:00 List-Id: Jacob Sparre Andersen wrote: > * Why are the two programs below not equivalent? > * How can I get the Ada program to run as fast as the C++ > program? > > Ada: > > Compilation...: gnatmake sqrt_timer -O3 -gnatn > Execution time: 3460 ms > Source code: > > with Ada.Numerics.Generic_Elementary_Functions; > with Ada.Text_IO; > with Interfaces.C; > > procedure Sqrt_Timer is > > type Scalar is new Interfaces.C.Double; > > package Math is new > Ada.Numerics.Generic_Elementary_Functions (Scalar); > > function Sqrt (Item : in Scalar) return Scalar renames > Math.Sqrt; > pragma Inline (Sqrt); > > X : Scalar := 2.0; > > begin -- Sqrt_Timer > for i in 1 .. 10_000_000 loop > X := Sqrt (1.0 + X); > end loop; > > Ada.Text_IO.Put_Line ("X = " & Scalar'Image (X)); > end Sqrt_Timer; > > C++ > === > > Compilation...: g++ sqrt_timer.cpp -O3 > Execution time: 1460 ms > Source code: > > #include > #include > > int main (int argl,char* argv[]) { > > double x=2.0; > > for(int i=0;i<10000000;i++){ > x=sqrt(1.0+x); > }; > > cout << x << endl; > }; Turn off runtime checks in Ada, or add them to the C++. Ada's Sqrt is pickier than C/++'s; use the C sqrt from Ada. C++'s for is really a while preceded by an assignment with an increment added at the end; use the same structure in Ada. Make C++ output "X = ", too. -- Jeffrey Carter