Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Inline declaration inside loop and performance

ravi_lanjewar
Contributor
0 Kudos

I have seen SAP ABAP support inline declaration after release ABAP 7.4.

I have declare the inline statement inside the loop.


loop at it_data.

data(lv_val) = 5.

endloop.

In another example

I have declared the statement without inline declaration in side loop.


data(ls_val) = 5.

loop at it_data.

lv_val = 5 .

endloop.

This is very simple example but in case of declaration of internal table inside loop verses outside inside.

I don't know how the inline declaration internally work in ABAP 7.4


Which will give better performance and why ?

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert

As the documentation says

  • A valid statement with an inline declaration of a variable can generally be interpreted as a short form for a declaration statement used as a direct prefix.
DATA var TYPE ...
... var ...

Why should that be less performant?

Declarations - whether inline or not - are evaluated at compile time not at runtime!!!

You don't need a test for that simple truth ...

8 REPLIES 8

former_member192616
Active Contributor
0 Kudos

Hi,


Which will give better performance and why ?

Regarding the first part of your question:

Try it out and write a little testprogram and meassure it with SAT and please share the results with us. If there is a significant difference we can continue with the discussion...

Kind regards,

Hermann

0 Kudos

Hi Hermann,

Agree with your suggestion. I will post the result.

KR.

Ravishankar

Former Member
0 Kudos

Hi,

it seems that both variants are equal.

Two FORMS, one with inline declaration in a loop, one with the inline declaration before the loop.

For significance I declared in both cases 10 variables and I used a DO 10^7 TMES loop for both variants.

The whole testcase needed 7 seconds.

The "In Loop declaration" form needed 3,499 Seconds

The "Out of Loop declaration" form needed also 3,499 seconds.

I did several measurments and the runtime of those two methods didn't change much.

The biggest difference between both was around 0,005 seconds. Not very significant, I think.

So my conclusion is:

To declare a variable in or before a loop does not have any performance inpact.

warm regards

Jan

horst_keller
Product and Topic Expert
Product and Topic Expert

As the documentation says

  • A valid statement with an inline declaration of a variable can generally be interpreted as a short form for a declaration statement used as a direct prefix.
DATA var TYPE ...
... var ...

Why should that be less performant?

Declarations - whether inline or not - are evaluated at compile time not at runtime!!!

You don't need a test for that simple truth ...

0 Kudos

*smile*

I made the mistake and forgot that ABAP is simple. 😉

I thought the inline declaration  determines the data type at runtime, like some other languages, so that it can be used for already declared and for generic data.

Type determination at runtime could (or could not) have an impact at the runtime, depending on the implementation of the compiler.

Again i tested the simple truth and had to learn, that inline declaration can´t be used with generic data types.

Another day goes by, another thing learned. 🙂

By the way, i personaly like to test simple truths. To often the truth is hiden behind obscuring obviousness.

Even if sometimes the world is truly as easy as it looked like. 😉

warm regards

Jan

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos


Again i tested the simple truth and had to learn, that inline declaration can´t be used with generic data types.


Not?


FIELD-SYMBOLS <fs> TYPE csequence.

DATA(dobj) = <fs>.


-> dobj hast type string, see assignment of data objects.


CLASS demo DEFINITION.

   PUBLIC SECTION.

     CLASS-METHODS main EXPORTING p TYPE numeric.

ENDCLASS.

CLASS demo IMPLEMENTATION.

   METHOD main.

   ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

   demo=>main( IMPORTING p = DATA(dobj) ).


-> dobj has type decfloat64, see parameter passing.


Or what do you mean?


Cheers!


Horst

0 Kudos

Hi,

with generic i meant that not only the content but also the type of a variable is only known at runtime

In your examples, the data type is "known" at compile time, because the assigned data is of a defined type (csequence and numeric).

I tried it with a assignment of a generic variable, where the type is not known at compile time .

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

DATA: type_char TYPE char5.

FIELD-SYMBOLS: <type_data> TYPE data.

type_char = 'TEST'.

ASSIGN type_char TO <type_data>.

DATA(new_var) = <type_data>.

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

In this case <type_data> is generic and could be of any type. When i try to compile it, i get an error message, that the generic type couldn´t be used for declaration.

If we build this example a little bit mor complex, the compiler doesn´t have any chance to know which type the field-symbol will have at compile time.

So you are completly right, the inline declaration declares a variable at compile time. And then it is logical that it is not peformance relevant if the ariable is declared in or out of a loop. 🙂

Warm regards

Jan

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
In this case <type_data> is generic and could be of any type. When i try to compile it, i get an error message, that the generic type couldn´t be used for declaration.

Right, that's what I mean with the sentence "It must be possible to derive this type statically in full."

Any proposal, how to better express this "(not so) simple truth" ?

You already find a hint "If the type of a different operand cannot be identified statically (perhaps because it is specified as a generically typed field symbol), either a suitable standard type is used or no inline declaration is possible."

Kind regards

Horst