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-Thread: a07f3367d7,c8acfc87fbb1813d X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!83.128.0.12.MISMATCH!news-out2.kabelfoon.nl!newsfeed.kabelfoon.nl!xindi.nntp.kabelfoon.nl!news.banetele.no!uio.no!fi.sn.net!newsfeed2.tdcnet.fi!news.song.fi!not-for-mail Date: Fri, 05 Mar 2010 10:21:43 +0200 From: Niklas Holsti Organization: Tidorum Ltd User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Timing code blocks References: <2d5bead9-72f9-4c84-9ac1-a058c2591ef1@c37g2000prb.googlegroups.com> In-Reply-To: <2d5bead9-72f9-4c84-9ac1-a058c2591ef1@c37g2000prb.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4b90bf24$0$1979$4f793bc4@news.tdc.fi> NNTP-Posting-Host: 81.17.205.61 X-Trace: 1267777316 news.tdc.fi 1979 81.17.205.61:52483 X-Complaints-To: abuse@tdcnet.fi Xref: g2news1.google.com comp.lang.ada:9416 Date: 2010-03-05T10:21:43+02:00 List-Id: deadlyhead wrote: > I've been trying to determine if there is any significant performance > difference between two functionally equivalent pieces of code. I've > done the standard thing and, using the Ada.Real_Time package, I'm > saving the time when the code starts, then the time when the code > ends, then examining the difference between them after the code runs. > > The problem I'm having, though, is that the timing just isn't > happening. This code will run for 15 seconds and when I examine the > time span, it tells me that no time passed. > > Here's my actual code [ code elided ] Dmitry's already answered your main question. But your code also ha some other issues that I would like point out, in a friendly spirit. I have tried your code on Debian Lenny with the Debian GNAT compiler. Firstly, if I compile with my normal options, -g -O2 -gnato -fstack-check, the code fails after some seconds with Constraint_Error due to overflow in the assignment Junk_In := Junk_In + 1 on line 42 (counting the line with all '-' as line 1). This is expected, since this statement would be executed about (10**10)/2 times, leading to overflow with a 32-bit Natural counter. Secondly, if I compile without overflow checks (-g -O2) the program runs very quickly and displays ---Starting per-branch assignment test--- Assignment within each branch took 0.000006000000 ---Starting combined-branch assignment test--- Assignment outside of the branching took 0.000002000000 I believe that GNAT optimizes out almost everything in your loops, because the results are not used in the program. To disable some of this optimization you can use pragma Volatile to force GNAT to generate code that actually executes all the accesses to the variables, for example like this: Is_Even : Boolean; pragma Volatile (Is_Even); Is_Odd : Boolean; pragma Volatile (Is_Odd); Junk : Positive; pragma Volatile (Junk); Junk_In : Natural := 0; pragma Volatile (Junk_In); With this change, and compiling with "-g -O2", the code takes about 1 minute 30 seconds to run on my laptop and displays: ---Starting per-branch assignment test--- Assignment within each branch took 44.178160000000 ---Starting combined-branch assignment test--- Assignment outside of the branching took 45.378627000000 I don't think you are not going to find valid performance differences between different styles of code using this kind of artificial test programs, because the compiler's code generation and optimization make such profound changes to the code, depending on the surroundings, data types, etc. If you have a real performance problem in a real application, I suggest that you experiment with changes to the real application code. Use a profiler to find out where the time is spent and focus on that code. HTH, -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .