1

I have the following records:

type Items is
   record
      Feature_A : Type_Feature_A;
      Feature_B : Type_Feature_B;
      Feature_C : Type_Feature_C;
      Feature_D : Type_Feature_D;
   end record;

type My_Array is array (1 .. 10) of Items;

type My_Record is
   record
      Count    : Integer;
      Item_Arr : My_Array;
   end record;

I worked with the type My_Record, for example I stored 8 items in the array and then I want to extract items 2 by 2.

To do that I have declared a function that copy the 2 first items of the array to a subtype of My_Array and decrement 'Count' by 2. Then I slice 'Item_Arr':

Item_Arr(1..8) := Item_Arr(3..10);
Item_Arr(9..10) := Default_Value;

My question is: Is it the proper way to do that? Or it will be better to loop over items and each times I found an item different from the 'Default_Value' I added it to the output array and so on.

In terms of performance, comparing records is faster than slicing an array?

Thank you

user2205092
  • 83
  • 1
  • 7
  • Better to use a variable array slice by varying the indices and leaving the data in place rather than copying the entire array. –  Oct 17 '16 at 14:19

1 Answers1

1

As always when the question is performance, the answer is "measure, measure, measure".

Since Ada has been written with performance in mind, I consider it best practice to use the most readable implementation I can come up with as the reference implementation for performance measurements.

In the concrete case, I would say that your suggestion probably is the most readable implementation.

For performance, one alternative is to have a circular queue, so you don't move elements around, but instead keep track of both the head and the tail of the queue.

Jacob Sparre Andersen
  • 6,733
  • 17
  • 22