comp.lang.ada
 help / color / mirror / Atom feed
* Question about arrays and no. of elements
@ 2008-05-15 15:19 amal.alphonse
  2008-05-15 16:30 ` gautier_niouzes
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: amal.alphonse @ 2008-05-15 15:19 UTC (permalink / raw)


If I define an array with say a million elements, and I only put data
in 10 of them, is it actually a waste of space/time with the array or
does Ada only create elements when there is data to put in (so it is
efficient)?



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Question about arrays and no. of elements
  2008-05-15 15:19 Question about arrays and no. of elements amal.alphonse
@ 2008-05-15 16:30 ` gautier_niouzes
  2008-05-15 16:50 ` anon
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: gautier_niouzes @ 2008-05-15 16:30 UTC (permalink / raw)


On 15 Mai, 17:19, amal.alpho...@gmail.com wrote:
> If I define an array with say a million elements, and I only put data
> in 10 of them, is it actually a waste of space/time with the array or
> does Ada only create elements when there is data to put in (so it is
> efficient)?

When you don't know the size: either you waste time (a smart
extensible array), or you waste space (a simple array)...
This whatever the language.

G.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Question about arrays and no. of elements
  2008-05-15 15:19 Question about arrays and no. of elements amal.alphonse
  2008-05-15 16:30 ` gautier_niouzes
@ 2008-05-15 16:50 ` anon
  2008-05-15 17:52 ` Adam Beneschan
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: anon @ 2008-05-15 16:50 UTC (permalink / raw)


Arrays should be created for the number of element used in the 
program with allowances for normal growth of data though the 
lifecycle of the program.  For a array of 10 elements it might be 
easier and more efficient to request an array size that fits on a 
memory page bounds, such as array size that would fit into a 256 
or 512 sector page or a 1 to 4 KB block.

Efficiency of execution depending on how you allocate the array. Plus, 
Array are created before they can be used.

If the compiler creates the million element array then the OS will first 
have to allocate space and load the array into memory (real or virtual), 
if possible. Then the system will load the entire program with array 
into memory.

Now, if array is created during program initialization after loaded into 
memory.  The Ada run-time system will try to allocate the space by 
make a request to the OS for memory, if allocated, the run-time 
simply returns and program continues.  But if memory was not 
allocated then STORAGE_ERROR will be the result.


In <b60e9537-ab45-4856-99de-71127208813f@56g2000hsm.googlegroups.com>, amal.alphonse@gmail.com writes:
>If I define an array with say a million elements, and I only put data
>in 10 of them, is it actually a waste of space/time with the array or
>does Ada only create elements when there is data to put in (so it is
>efficient)?




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Question about arrays and no. of elements
  2008-05-15 15:19 Question about arrays and no. of elements amal.alphonse
  2008-05-15 16:30 ` gautier_niouzes
  2008-05-15 16:50 ` anon
@ 2008-05-15 17:52 ` Adam Beneschan
  2008-05-16  9:50 ` Ken Thomas
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Adam Beneschan @ 2008-05-15 17:52 UTC (permalink / raw)


On May 15, 8:19 am, amal.alpho...@gmail.com wrote:
> If I define an array with say a million elements, and I only put data
> in 10 of them, is it actually a waste of space/time with the array or
> does Ada only create elements when there is data to put in (so it is
> efficient)?

"Ada" the language doesn't really dictate how arrays are represented,
but I think that any existing Ada compiler will allocate storage for a
million elements.  This may be a waste of space, but I don't
understand why you think it would be a waste of time: it is *more*
efficient, time-wise, to do it this way, because determining the
location of a particular array element is very simple, while any
scheme that tries to allocate memory only for elements actually used
is going to require significant overhead.

                                -- Adam




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Question about arrays and no. of elements
  2008-05-15 15:19 Question about arrays and no. of elements amal.alphonse
                   ` (2 preceding siblings ...)
  2008-05-15 17:52 ` Adam Beneschan
@ 2008-05-16  9:50 ` Ken Thomas
  2008-05-17 14:20 ` Steve
  2008-05-17 16:32 ` Jacob Sparre Andersen
  5 siblings, 0 replies; 8+ messages in thread
From: Ken Thomas @ 2008-05-16  9:50 UTC (permalink / raw)


On May 15, 4:19 pm, amal.alpho...@gmail.com wrote:
> If I define an array with say a million elements, and I only put data
> in 10 of them, is it actually a waste of space/time with the array or
> does Ada only create elements when there is data to put in (so it is
> efficient)?

It will be a waste of space because you may limit the size of problem
you can solve with your resources.

It may be a waste of time if you perform operations on the whole array
such as scaling. You have only defined 10 elements and given default
values to the remaining 10^6 - 10 elements.

Why not use a more suitable data structure such as a Map
(Ada.Containers.Ordered_Maps)?
I have used these to handle large sparse matrices that cannot be
stored as dense arrays. I have not found any significant degradation
of runtime performance and I don't think you will for such a small
map.

Ken



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Question about arrays and no. of elements
  2008-05-15 15:19 Question about arrays and no. of elements amal.alphonse
                   ` (3 preceding siblings ...)
  2008-05-16  9:50 ` Ken Thomas
@ 2008-05-17 14:20 ` Steve
  2008-05-17 16:32 ` Jacob Sparre Andersen
  5 siblings, 0 replies; 8+ messages in thread
From: Steve @ 2008-05-17 14:20 UTC (permalink / raw)


<amal.alphonse@gmail.com> wrote in message 
news:b60e9537-ab45-4856-99de-71127208813f@56g2000hsm.googlegroups.com...
> If I define an array with say a million elements, and I only put data
> in 10 of them, is it actually a waste of space/time with the array or
> does Ada only create elements when there is data to put in (so it is
> efficient)?

Generally if you define an array with a million elements, most compiled 
languages (including Ada) will allocate memory for the array, which a waste 
of space, but not of time.

After the array is allocated, references to elements are just memory 
references which are fast.

Also note:
  Unlike some other programming languages, you may define the size of the 
array at run time, if you know the size of the array prior to use:

    type My_Array_Type is array( Positive range <> ) of Integer;
    ...
    num_elements : Positive;
    begin
        num_elements := GetNumElements;
        declare
            my_array_instance : My_Array_Type( 1 .. num_elements );
        begin
            ...
        end;

Regards
Steve
(The Duck) 





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Question about arrays and no. of elements
  2008-05-15 15:19 Question about arrays and no. of elements amal.alphonse
                   ` (4 preceding siblings ...)
  2008-05-17 14:20 ` Steve
@ 2008-05-17 16:32 ` Jacob Sparre Andersen
  2008-05-17 17:37   ` Ludovic Brenta
  5 siblings, 1 reply; 8+ messages in thread
From: Jacob Sparre Andersen @ 2008-05-17 16:32 UTC (permalink / raw)


amal.alphonse@gmail.com writes:

> If I define an array with say a million elements, and I only put
> data in 10 of them, is it actually a waste of space/time with the
> array or does Ada only create elements when there is data to put in
> (so it is efficient)?

With GNAT on Linux, you will only use memory for the part of the array
you put something into (modulo the page size - which I think is 4 kb).

Greetings,

Jacob
-- 
The backhoe is the natural predator of the fiber optic cable.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Question about arrays and no. of elements
  2008-05-17 16:32 ` Jacob Sparre Andersen
@ 2008-05-17 17:37   ` Ludovic Brenta
  0 siblings, 0 replies; 8+ messages in thread
From: Ludovic Brenta @ 2008-05-17 17:37 UTC (permalink / raw)


Jacob Sparre Andersen writes:
> amal.alphonse writes:
>
>> If I define an array with say a million elements, and I only put
>> data in 10 of them, is it actually a waste of space/time with the
>> array or does Ada only create elements when there is data to put in
>> (so it is efficient)?
>
> With GNAT on Linux, you will only use memory for the part of the array
> you put something into (modulo the page size - which I think is 4 kb).

Actually, that's a kernel feature (commit-on-write) independent of the
compiler.

-- 
Ludovic Brenta.



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-05-17 17:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-15 15:19 Question about arrays and no. of elements amal.alphonse
2008-05-15 16:30 ` gautier_niouzes
2008-05-15 16:50 ` anon
2008-05-15 17:52 ` Adam Beneschan
2008-05-16  9:50 ` Ken Thomas
2008-05-17 14:20 ` Steve
2008-05-17 16:32 ` Jacob Sparre Andersen
2008-05-17 17:37   ` Ludovic Brenta

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