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.3 required=5.0 tests=BAYES_00,FREEMAIL_FROM, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: anon@att.net Newsgroups: comp.lang.ada Subject: Re: Assuming optimization? What is best of these code alternatives? Date: Fri, 12 Sep 2014 06:17:30 +0000 (UTC) Organization: Aioe.org NNTP Server Message-ID: References: <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com> Reply-To: anon@att.net NNTP-Posting-Host: 90eFk/41J9XjlbLxowxWMw.user.speranza.aioe.org X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.2 X-Newsreader: IBM NewsReader/2 2.0 Xref: number.nntp.dca.giganews.com comp.lang.ada:188978 Date: 2014-09-12T06:17:30+00:00 List-Id: Actually since the equation ( i1 + i2 .. n ) for the "for statement" is evaluated once, this means that alternative 2 is the faster code. Because in alternative 1 the system perform must reference ( K ) at least once and possible twice plus provide storage for K. Now, for optimization in Ada, The RM provide a "pragma Optimize" with two options "Space" and "Time". But in GNAT "pragma Optimize" is non-functional, instead GNAT uses GCC backend option -OX where X is 0 to 4 at the movement to perform optimization. Now, since GNAT is a front-end to the compiler it is possible for GNAT to use "pragma Optimize ( Space ) ;" to remove source-level dead code. While "pragma Optimize ( Time ) ;" could turn on optimization in the backend to a preset default optimization or allow command-line option to override default. Note: GCC does not have an option to remove dead code from precompile libraries or routines during linking. In <0868c42e-ed44-4b36-a929-2bffb338ee34@googlegroups.com>, reinkor writes: >I am not sure to what extent one may assume the Ada compiler >makes optimization. For example, assume the following two program >code alternatives: > > >-----------------------Alternative 1: > i1,i2,k : Integer; >begin > (Read i1,i2 and n - do not change after) > > k := i1 + i2; -- store "i1 + i2" in k to avoid recalculation (?) > loop > for i in k .. n loop > Do something... > end loop; > end loop; > >------------------------Alternative 2: > i1,i2 : Integer; >begin > (Read i1, i2 and n - do not change after) > > loop > for i in i1 + i2 .. n loop -- will recalculate "i1 + i2" ? > Do something... > end loop; > end loop; > >What is the "best" alternative? Alternative 2 >is shortest (and probably easier to read), but >could it give slower code that for Alternative 1 ? > >Yes, this example is somehow over-simplified :-) > >