Application Development and Automation 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: 
Read only

loop with in loop

Former Member
0 Likes
627

hi experts,

can any body tell me how can a loop with in loop effect the performance of the abap program.

can we use like that .

5 REPLIES 5
Read only

RaymondGiuseppi
Active Contributor
0 Likes
590

Usually "loop in loop" looks like

LOOP AT header_itab.
  LOOP AT item_itab WHERE num = header_itab-num.
  ENDLOOP.
ENDLOOP.

With standard (eg index) tables the item_tab can be fully read to select records at each pass. Only way to optimize is to have declared this table as a SORTED TABLE with the header number as a key. In this case the internal loop is optimized. - This is written in online abap documentation.

Regards

Read only

0 Likes
590

thanks a lot for ur reply.

Read only

Former Member
0 Likes
590

loop at itab1. ( n records)

loop at itab2. (m records)

endloop.

endloop.

for each record of itab1. entire itab2 is looping so finally inner loop is going to run n * m times.

so it is very poor performance

to avoid this u need to use read statement

Read only

0 Likes
590

thanks a lot for ur reply.

Read only

Former Member
0 Likes
590

If the question is already answered then you forgot to give points!

There is general assumption that you have either

loop at itab1. ( n records)

loop at itab2. ( a records, a constant)

or

loop at itab1

loop at itab2 where key = (m records in itab2, where m grows if n grows)

In the first case you have no performance problem, it scales with n.

In the second case, you have a performance problem if itab2 is standard table. But you can avoid it, if you use sorted tables.

You can also avoid it for standard tables by a workaround

read binary search, loop from index, exit

For details on the workaround see

Measurements on internal tables: Reads and Loops:

/people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables

For background information on nonlinearity see

/people/siegfried.boes/blog/2007/02/12/performance-problems-caused-by-nonlinear-coding

Siegfried