comp.lang.ada
 help / color / mirror / Atom feed
From: deadlyhead <deadlyhead@gmail.com>
Subject: Timing code blocks
Date: Thu, 4 Mar 2010 22:34:47 -0800 (PST)
Date: 2010-03-04T22:34:47-08:00	[thread overview]
Message-ID: <2d5bead9-72f9-4c84-9ac1-a058c2591ef1@c37g2000prb.googlegroups.com> (raw)

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

----------------------------------------------------------------------

--  A test of the cost of conditionals

with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Real_Time; use Ada.Real_Time;
procedure Conditional_Test is

   Test_Dur : constant := 100_000;

   -- We require the input/output for Duration
   package Duration_IO is new Fixed_IO (Duration);
   use Duration_IO;


   Start_Time : Time;
   End_Time   : Time;
   Is_Even    : Boolean;
   Is_Odd     : Boolean;
   Junk       : Positive;
   Junk_In    : Natural := 0;

begin   -- Conditional_test

   Put_Line ("---Starting per-branch assignment test---");

   Start_Time := Clock;
   for I in 1 .. Test_Dur loop
      if I rem 2 = 1 then
         Is_Odd  := True;
      else
         Is_Even := True;
      end if;

      if Is_Even then
         for J in reverse 1 .. Test_Dur loop
            Junk_In := Junk_In + 1;
         end loop;
         Junk := I;
      elsif Is_Odd then
         for J in reverse 1 .. Test_Dur loop
            Junk_In := Junk_In + 1;
         end loop;
         Junk := I;
      end if;

      Is_Even := False;
      Is_Odd  := False;
   end loop;
   End_Time := Clock;

   Put ("Assignment within each branch took ");
   Put (To_Duration (End_Time - Start_Time), 1, 12, 0);
   New_Line (2);


   Put_Line ("---Starting combined-branch assignment test---");

   Start_Time := Clock;
   for I in 1 .. Test_Dur loop
      if I rem 2 = 1 then
         Is_Odd  := True;
      else
         Is_Even := True;
      end if;

      if Is_Even then
         for J in reverse 1 .. Test_Dur loop
            Junk_In := Junk_In + 1;
         end loop;
      elsif Is_Odd then
         for J in reverse 1 .. Test_Dur loop
            Junk_In := Junk_In + 1;
         end loop;
      end if;

      if Is_Even or Is_Odd then
         Junk := I;
      end if;

      Is_Even := False;
      Is_Odd  := False;
   end loop;
   End_Time := Clock;

   Put ("Assignment outside of the branching took ");
   Put (To_Duration (End_Time - Start_Time), 1, 12, 0);
   New_Line (2);

end Conditional_Test;

----------------------------------------------------------------------

The output of this code is as follows:


---Starting per-branch assignment test---
Assignment within each branch took 0.000000000000

---Starting combined-branch assignment test---
Assignment outside of the branching took 0.000000000000


Why wouldn't any time passage be registered?

I know the code above is convoluted, but I've been trying to find a
way to get _some_ timing to happen. I originally ran a test with delay
statements instead of multiple loops, and the timing worked then, but
I felt that there was a real possibility that the delay statements
could be introducing inaccuracies into the timing, with the overhead
of switching processes in the OS and there being no guarantee of
consistently resuming the code. (Ada does not guarantee that code will
resume exactly delay_time from now, it guarantees that the code will
sleep for _at least_ delay_time.)

Anyway, if I'm missing something here, I'd like to know it.  I've read
section D.8 of the ARM several times and I'm just about convinced that
something's broken in my compilers (I'm using GNAT on windows, both
the AdaCore binary and a cygwin binary, both with the same output). Is
there something that I'm missing, like the real-time clock doesn't
advance unless the program delays at some point?

I appreciate any insight.  This is baffling me.



             reply	other threads:[~2010-03-05  6:34 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-05  6:34 deadlyhead [this message]
2010-03-05  7:55 ` Timing code blocks 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
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