comp.lang.ada
 help / color / mirror / Atom feed
From: czgrr <czgrr@my-dejanews.com>
Subject: Re: scope and/or parameters (beginner)
Date: 1999/04/14
Date: 1999-04-14T00:00:00+00:00	[thread overview]
Message-ID: <7f1jce$nt4$1@nnrp1.dejanews.com> (raw)
In-Reply-To: 7evbei$opm$1@nnrp1.dejanews.com



In article <7evbei$opm$1@nnrp1.dejanews.com>,
  Robert Dewar <robert_dewar@my-dejanews.com> wrote:
> In article <whatever>, czgrr <czgrr@my-dejanews.com> wrote:
> > So there is a definite difference between the time taken
> > to call a routine with and without local variables,
> > albeit very small.
>
> This is plain wrong on many machines. There is no
> difference. Furthermore, just because you don't WRITE
> any local variables does not mean there are none in the
> generated code.
[snip]
> > So making as much as you can be
> > global will improve overall run-time performance
>
> This is in general quite wrong. Access to local variables
> on the stack is generally much more efficient than access
> to global variables on modern architectures.
>
[taken from the end of the post]
> Whether something needs to be global or local (in lifetime
> or scope) is a logic issue in a program, and not an
> efficiency issue that a programmer should worry about.
>
At the end of this post I have included some code. The output I get is also
listed, and you can clearly see a difference between the two sets of procedure
calls, with the only code difference being a variable declared locally or
globally. I admit the local version is not of much use!

Now the difference may be due to something to do with the machine, the
memory, the compiler, bind options, etc, but the point I was making is that
there *can* be a difference.

Yes, in an ideal world the programmer shouldn't have to worry about machine
implementations, but this is not an ideal world and if the application being
developed is real-time critical, the code given below could make the
difference between passing acceptance tests and failing.

I said this in my original post, and I say it again, I am not saying that
every variable, procedure, etc, should be made global just because it (may
be) quicker. Definitely not. Logic issues should determine whether
declarations are made local or global, but there are occasional circumstances
where practicalities must override the ideal logic. The code below is one of
them.

[snip]
> Indeed since you cannot use "new" with procedures except
> in a generic context it is not clear what you are talking
> about at all.

I was talking about generics, sorry, I jumped contexts there without
explaining. I tried what I was talking about on my compiler and, as you say,
it made no difference. However, several years ago when I was developing a
disk access package with lots of "NEW TEXT_IO.FLOAT_IO" kind of declarations,
it definitely made a difference putting them all together at the top of the
package rather than individually within each procedure where needed.

Again, it may have been the compiler, whatever. But it did matter, and I don't
believe that programmers should be unaware of these possibilities.

**********

Now, the code I was talking about...

PACKAGE temp IS
  max : INTEGER := 1e5 ;
  PROCEDURE global ( index : IN INTEGER ) ;
  PROCEDURE local ( index : IN INTEGER ) ;
END temp ;

-----

PACKAGE BODY temp IS
  big_array_global : ARRAY ( 1 .. max ) OF INTEGER ;

  PROCEDURE global ( index : IN INTEGER ) IS
  BEGIN
    big_array_global ( index ) := 0 ;
  END global ;

  PROCEDURE local ( index : IN INTEGER ) IS
    big_array_local : ARRAY ( 1 .. max ) OF INTEGER ;
  BEGIN
    big_array_local ( index ) := 0 ;
  END local ;

END temp ;

-----

WITH CALENDAR ;
WITH TEXT_IO ;
WITH temp ;

PROCEDURE main IS
  PACKAGE float_io IS NEW TEXT_IO.FLOAT_IO ( FLOAT ) ;

BEGIN
  float_io.PUT ( FLOAT ( CALENDAR.SECONDS ( CALENDAR.CLOCK ) ) ) ;
  TEXT_IO.PUT_LINE ( " Calling global..." ) ;
  FOR i IN 1 .. temp.max LOOP
    temp.global ( i ) ;
  END LOOP ;

  float_io.PUT ( FLOAT ( CALENDAR.SECONDS ( CALENDAR.CLOCK ) ) ) ;
  TEXT_IO.PUT_LINE ( " Calling local..." ) ;
  FOR i IN 1 .. temp.max LOOP
    temp.local ( i ) ;
  END LOOP ;

  float_io.PUT ( FLOAT ( CALENDAR.SECONDS ( CALENDAR.CLOCK ) ) ) ;
  TEXT_IO.PUT_LINE ( " Finished." ) ;
  DELAY ( 100.0 ) ;

END main ;

**********

I use ObjectAda Professional Edition version 7.1.1.543, on Windows NT 4.0 with
128MB memory. Output was...

3.67944E+04 Calling global...
3.67945E+04 Calling local...
3.68065E+04 Finished.

That is, 0.1 second (to nearest 0.1 second!) to do the global verison, 12.0
seconds to do the local version.

czgrr

--
My opinions, suggestions, etc, are not necessarily those of my employer.
They might not even be right. Use at your own risk.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




  reply	other threads:[~1999-04-14  0:00 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-04-02  0:00 scope and/or parameters (beginner) fluffy_pink
1999-04-03  0:00 ` Matthew Heaney
1999-04-05  0:00 ` Corey Ashford
1999-04-05  0:00   ` fluffy_doo
1999-04-06  0:00     ` Matthew Heaney
1999-04-08  0:00     ` czgrr
1999-04-10  0:00       ` fluffy_puff
1999-04-12  0:00       ` dennison
1999-04-13  0:00         ` czgrr
1999-04-13  0:00           ` Robert Dewar
1999-04-14  0:00             ` czgrr [this message]
1999-04-14  0:00               ` dennison
1999-04-14  0:00               ` Robert Dewar
1999-04-15  0:00                 ` czgrr
1999-04-15  0:00                   ` Robert Dewar
1999-04-13  0:00         ` Robert Dewar
1999-04-13  0:00     ` Robert A Duff
1999-04-14  0:00       ` Robert Dewar
1999-04-14  0:00         ` Hyman Rosen
1999-04-14  0:00           ` dennison
1999-04-14  0:00             ` Hyman Rosen
1999-04-14  0:00               ` dennison
1999-04-14  0:00                 ` Hyman Rosen
1999-04-15  0:00                   ` dennison
1999-04-15  0:00                     ` Robert Dewar
1999-04-15  0:00                       ` dennison
1999-04-15  0:00                       ` Hyman Rosen
1999-04-15  0:00           ` Robert Dewar
1999-04-15  0:00             ` Hyman Rosen
1999-04-15  0:00               ` Robert Dewar
1999-04-15  0:00                 ` Hyman Rosen
1999-04-16  0:00               ` Rakesh Malhotra
1999-04-15  0:00       ` fluffy_dong
1999-04-15  0:00         ` Robert Dewar
1999-04-15  0:00           ` dennison
1999-04-15  0:00             ` fluffy_dong
1999-04-16  0:00               ` Robert Dewar
1999-04-16  0:00                 ` Fraser Wilson
1999-04-16  0:00                   ` Gautier.DeMontmollin
1999-04-20  0:00                     ` Nick Roberts
1999-04-21  0:00                     ` fraser
1999-04-22  0:00               ` Robert A Duff
1999-04-22  0:00                 ` Larry Kilgallen
1999-04-16  0:00         ` Samuel Mize
replies disabled

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