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

improving loop performance

sudha_naik
Product and Topic Expert
Product and Topic Expert
0 Likes
598

hello

I have a nested loop which loops through two itabs. Since there are too many records in both the itabs it is taking too much of time. Could anyone please suggest some ways to improve this code so that it takes less time.

Two itabs are itab1 and itab2.

LOOP AT itab1.

CLEAR L_TABIX.

APPEND itab1 TO vsel.

READ TABLE itab2 WITH KEY cuobj = itab1-cuobj binary search.

IF sy-subrc NE 0.

CONTINUE.

ELSE.

l_tabix = sy-tabix.

LOOP AT itab2 FROM l_tabix.

IF itab2-cuobj NE itab1-cuobj.

EXIT.

ENDIF.

READ TABLE ymodes WITH KEY mode = itab2-mode.

IF sy-subrc EQ 0.

IF itab2-wtsta NE ' ' AND

CLEAR itab2-wtsta.

ENDIF.

APPEND itab2 TO wsel.

ENDIF.

ENDLOOP.

ENDIF.

ENDLOOP.

Regards

Sudha

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
570

hi,

Use binary search for READ TABLE ymodes.

And also cross check wheather itab1, itab2, ymodes are sorted or not with required key.

<b>itab1</b> may contain multiple records with same <b>cuobj</b> value. so, in that case same code is processed multiple times for same values. Try to avoid this according to the data present in the tables.

Loop at where takes much longer time for tables with large number of records as it reads complete table for each loop pass of the given condition in where clause.

Regards,

Sankar

5 REPLIES 5
Read only

Former Member
0 Likes
570

Hi Sudha,

PLease use the following code if itab1 table use the primare data of cuobj.

<b>loop at itab1.

loop at itab 2 where cuobj = itab1-cuobj .

do your necessary steps over here.

endloop.

endloop.</b>

It will definitely improve performance.

<b>Rewards points if it is useful.</b>

Regards,

Kinjal

Read only

Former Member
0 Likes
570

Hi,

please check whether you could use a sorted table for itab2, would make your

codingg simpler, but not faster. Onyl then the above loop at where works

performantly. The nested loops are not recommended for standard tables!

If a sorted table is not possible, then itab must be sorted outside of loop at itab1,

this is not noted in your example.

The rest of your example is o.k., use binary search to find starting point,

loop from index, exit if itab2-cuobj NE itab1-cuobj.

This can not be the problem, maybe you expect to much.

How about READ TABLE ymodes WITH KEY mode = itab2-mode,

how large is ymode? If it is more than 20 lines, then it would be a good

idea to have a hashed table here as this read is executed inside the nested loops.

Use SE30 to check the runtime.

Siegfried

Read only

Former Member
0 Likes
571

hi,

Use binary search for READ TABLE ymodes.

And also cross check wheather itab1, itab2, ymodes are sorted or not with required key.

<b>itab1</b> may contain multiple records with same <b>cuobj</b> value. so, in that case same code is processed multiple times for same values. Try to avoid this according to the data present in the tables.

Loop at where takes much longer time for tables with large number of records as it reads complete table for each loop pass of the given condition in where clause.

Regards,

Sankar

Read only

Former Member
0 Likes
570

Sankar is right, but you have to careful.

You don't have to sort itab1, the loop processes anyway everything.

Checking for duplicates is probabyl more expensive then the processing

of a few duplicates.

Siegfriied

Read only

Former Member
0 Likes
570

Look at the parallel cursor method in <a href="/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops of Nested Loops</a>. Basically:

sort ITAB1 and ITAB2 by a key (CUOBJ).
LOOP through ITAB1.
  LOOP through ITAB2 FROM INDEX  XYZ.
    XYZ  =  SY-TABIX.
    IF  ITAB2-KEY  >  ITAB1-KEY.
      EXIT.
    ELSEIF ITAB1-KEY  >  ITAB2-KEY.
      CONTINUE.
    ENDIF.
    your process.
  ENDLOOP.
ENDLOOP.

Additionally sort YMODES and use a BINARY SEARCH for your read.

MattG.