From: kongra on
Hi everybody.

I have a question related to the Ada.Real_Time package. When I compile
the following code using GNAT GPL 2007 on Windows XP SP2 I observe
some strange looking behavior.

with Ada.Real_Time;
with Ada.Text_IO;

procedure Timing is
Start_T : Ada.Real_Time.Time;
End_T : Ada.Real_Time.Time;
Total_T : Duration;
Sleep_T : constant Duration := Duration (0.0001);

use Ada.Real_Time;

Val : Integer := 0;
begin
-- delay Sleep_T;
Start_T := Ada.Real_Time.Clock;
for K in 1 .. 10 loop
for I in 1 .. 10_000_000 loop
Val := Val + I;
end loop;
end loop;
End_T := Ada.Real_Time.Clock;
Total_T := Ada.Real_Time.To_Duration (End_T - Start_T);
Ada.Text_IO.Put_Line (Integer'Image (Val));
Ada.Text_IO.Put_Line (Duration'Image (Total_T));
end Timing;

The compilation command is pretty straightforward: gnatmake Timing.adb
-o timing.exe. Running timing.exe I get

1432236160
0.000000000

To my surprise the total time is 0 here, though performing all the
computations takes some observable time. However removing comment in
the line containing statement delay Sleep_T; causes

1432236160
0.431441426

Apparently the total duration seems correct now. I get a similar
change by putting

with Ada.Calendar;

to the compilation unit (without completely using it). Moreover the
delay statement has got the same impact on the program execution even
when it's placed in some procedure other than Timing and the procedure
is not used at all.

I tried the same code in a bigger project using switches like those in
the Ada Wikibooks examples, getting the same.

Can it be explained or is there something I don't know about timing in
Ada ? I'm a beginner in Ada (after 7+ years Java coding) and I think
it's a great programming language, worth to be promoted among
students. But this one really surprised me even if I was able to avoid
using Ada.Calendar (for example prohibited by Ravenscar profile) using
a hack with delay.

Best regards,

Konrad Grzanek.
From: george.priv on
On Mar 29, 9:51 am, kongra <kon...(a)gmail.com> wrote:
> Hi everybody.
>
> I have a question related to the Ada.Real_Time package. When I compile
> the following code using GNAT GPL 2007 on Windows XP SP2 I observe
> some strange looking behavior.
>
> with Ada.Real_Time;
> with Ada.Text_IO;
>
> procedure Timing is
> Start_T : Ada.Real_Time.Time;
> End_T : Ada.Real_Time.Time;
> Total_T : Duration;
> Sleep_T : constant Duration := Duration (0.0001);
>
> use Ada.Real_Time;
>
> Val : Integer := 0;
> begin
> -- delay Sleep_T;
> Start_T := Ada.Real_Time.Clock;
> for K in 1 .. 10 loop
> for I in 1 .. 10_000_000 loop
> Val := Val + I;
> end loop;
> end loop;
> End_T := Ada.Real_Time.Clock;
> Total_T := Ada.Real_Time.To_Duration (End_T - Start_T);
> Ada.Text_IO.Put_Line (Integer'Image (Val));
> Ada.Text_IO.Put_Line (Duration'Image (Total_T));
> end Timing;
>
> The compilation command is pretty straightforward: gnatmake Timing.adb
> -o timing.exe. Running timing.exe I get
>
> 1432236160
> 0.000000000
>
> To my surprise the total time is 0 here, though performing all the
> computations takes some observable time. However removing comment in
> the line containing statement delay Sleep_T; causes
>
> 1432236160
> 0.431441426
>
> Apparently the total duration seems correct now. I get a similar
> change by putting
>
> with Ada.Calendar;
>
> to the compilation unit (without completely using it). Moreover the
> delay statement has got the same impact on the program execution even
> when it's placed in some procedure other than Timing and the procedure
> is not used at all.
>
> I tried the same code in a bigger project using switches like those in
> the Ada Wikibooks examples, getting the same.
>
> Can it be explained or is there something I don't know about timing in
> Ada ? I'm a beginner in Ada (after 7+ years Java coding) and I think
> it's a great programming language, worth to be promoted among
> students. But this one really surprised me even if I was able to avoid
> using Ada.Calendar (for example prohibited by Ravenscar profile) using
> a hack with delay.
>
> Best regards,
>
> Konrad Grzanek.

Your program should overflow (unless you suppress checks) raising
Constraint_Error for Val. It runs fine on Vista though after fixing
overflow problem

George.
From: Simon Wright on
Works fine with GNAT GPL 2007 on Powerbook. It takes the same time
(more-or-less) whether the initial delay is present or not.

I think it takes marginally longer if the overflow error is present,
not sure why that would be.

The way GNAT approaches Ada.Real_Time differs from platform to
platform -- I'm pretty sure that VxWorks used to use the same
mechanism under the hood, which made for problematic behaviour if the
POSIX time was altered. You may have come across some interaction
between Ada.Real_Time and the elaboration of Ada.Calendar on Windows.
From: Dmitry A. Kazakov on
On Sat, 29 Mar 2008 06:51:58 -0700 (PDT), kongra wrote:

> I have a question related to the Ada.Real_Time package.

[...]

> Sleep_T : constant Duration := Duration (0.0001);

You do don't need type conversion here:

Sleep_T : constant Duration := 0.0001; -- This is OK

[...]

> Can it be explained or is there something I don't know about timing in
> Ada ?

I remotely remember a discussion about this compiler bug It was discussed
in comp.lang.ada some time ago. I also remember that some people mentioned
tasking problems also circumvented by an initial delay. Which BTW can be
just 0.0;

My explanation is that GNAT RTL is improperly elaborated under Windows.
Fortunately, a delay at the program beginning wakes it up.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: george.priv on
On Mar 29, 1:29 pm, Georg Bauhaus <rm.tsoh.plus-
bug.bauh...(a)maps.futureapps.de> wrote:
> george.p...(a)gmail.com wrote:
> > Your program should overflow (unless you suppress checks) raising
> > Constraint_Error for Val. It runs fine on Vista though after fixing
> > overflow problem
>
> The overflow fix (+ I <--> + 1 ?), and also running GNAT in Ada
> mode (-gnato). Both do not fix the Total_T = 0.0 for me though.
> Vista 64bits.
>
> GNATLS GPL 2007 (20070405-41)
> Copyright 1997-2007, Free Software Foundation, Inc.
>
> The Debian GNAT compiler produces expected results, though.
>
> GNATLS 4.1.220061115prerelease (Debian 4.1.1-22)
> Copyright 1997-2005 Free Software Foundation, Inc.

Checked again after rebooting and works fine (Vista Ultimate: Version
6.0.6000) Gateway 2000 Tablet.
Takes 4 times longer with range checking (-O2).

C:\Users\gpriv.AXONX\Documents\ada>main
100000000
0.101334388

C:\Users\gpriv.AXONX\Documents\ada>main
100000000
0.429672137

George.