comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Timing code blocks
Date: Fri, 05 Mar 2010 10:21:43 +0200
Date: 2010-03-05T10:21:43+02:00	[thread overview]
Message-ID: <4b90bf24$0$1979$4f793bc4@news.tdc.fi> (raw)
In-Reply-To: <2d5bead9-72f9-4c84-9ac1-a058c2591ef1@c37g2000prb.googlegroups.com>

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
       .      @       .



  parent reply	other threads:[~2010-03-05  8:21 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-05  6:34 Timing code blocks deadlyhead
2010-03-05  7:55 ` Dmitry A. Kazakov
2010-03-05  8:16   ` deadlyhead
2010-03-05  8:49     ` Dmitry A. Kazakov
2010-03-05 12:41       ` Alex Mentis
2010-03-05 21:15         ` Jeffrey R. Carter
2010-03-05 23:35         ` Simon Wright
2010-03-06  9:50           ` Georg Bauhaus
2010-03-06 12:06             ` Simon Wright
2010-03-07  1:02               ` Georg Bauhaus
2010-03-08 12:16                 ` Alex Mentis
2010-03-06 12:12           ` Alex Mentis
2010-03-05  8:33   ` Ludovic Brenta
2010-03-05  9:04     ` Dmitry A. Kazakov
2010-03-05 15:27       ` Reporting bugs (was: Timing code blocks) Ludovic Brenta
2010-03-06  7:13         ` Stephen Leake
2010-03-05 23:35   ` Timing code blocks Simon Wright
2010-03-05  8:21 ` Niklas Holsti [this message]
2010-03-05 20:17   ` Simon Wright
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox