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

performance of a loop

sudha_naik
Product and Topic Expert
Product and Topic Expert
0 Likes
887

Hello All,

I have a nested loop which loops through two internal tables wsel and msel. Since there are thousands of records, this loop was taking time. It was found via ST12. Could anyone suggest way to improve this code so that it takes less time.

loop at msel where idxwt eq index.

loop at wsel where cuobj eq msel-cuobj

and mode eq ymodes-mode

and fwprd ne 0

and wtsta ne 'D'.

ssel-fsum = ssel-fsum + wsel-fwprd.

exit.

endloop.

endloop.

Thanks in advance

Sudha

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
850

Hi,

Better to use the index for loops and try below option.

If possible better to delete entries in wsel table where fwprd <> 0 and wtsta <> D otther if this table entries are required in future, you can dump this data in temp table and delete that entries.

then use index as below.

sort wsel by cuobj mode.

loop at msel where idxwt eq index.

read table wsel with key cuobj = msel-cuobj

mode = ymodes-mode

binary search.

if sy-subrc = 0.

ssel-fsum = ssel-fsum + wsel-fwprd.

exit.

endif.

endloop.

If multiple entries in table wsel, then use below model

loop at msel where idxwt eq index.

read table wsel with key cuobj = msel-cuobj

mode = ymodes-mode

binary search.

if sy-subrc = 0.

loop at wsel from sy-tabix.

if cuobj <> msel-cuobj or

mode <> ymodes-mode.

exit.

endif.

ssel-fsum = ssel-fsum + wsel-fwprd.

exit.

endloop.

endif.

endloop.

Hello All,

I have a nested loop which loops through two internal tables wsel and msel. Since there are thousands of records, this loop was taking time. It was found via ST12. Could anyone suggest way to improve this code so that it takes less time.

loop at msel where idxwt eq index.

loop at wsel where cuobj eq msel-cuobj

and mode eq ymodes-mode

and fwprd ne 0

and wtsta ne 'D'.

ssel-fsum = ssel-fsum + wsel-fwprd.

exit.

endloop.

endloop.

Thanks in advance

Sudha

4 REPLIES 4
Read only

Former Member
0 Likes
851

Hi,

Better to use the index for loops and try below option.

If possible better to delete entries in wsel table where fwprd <> 0 and wtsta <> D otther if this table entries are required in future, you can dump this data in temp table and delete that entries.

then use index as below.

sort wsel by cuobj mode.

loop at msel where idxwt eq index.

read table wsel with key cuobj = msel-cuobj

mode = ymodes-mode

binary search.

if sy-subrc = 0.

ssel-fsum = ssel-fsum + wsel-fwprd.

exit.

endif.

endloop.

If multiple entries in table wsel, then use below model

loop at msel where idxwt eq index.

read table wsel with key cuobj = msel-cuobj

mode = ymodes-mode

binary search.

if sy-subrc = 0.

loop at wsel from sy-tabix.

if cuobj <> msel-cuobj or

mode <> ymodes-mode.

exit.

endif.

ssel-fsum = ssel-fsum + wsel-fwprd.

exit.

endloop.

endif.

endloop.

Read only

Former Member
0 Likes
850

Hi,

In u r u can use read table inside first loop...endloop.

Whenever u use read table with key try to use binary search by sorting the internal table with that key.

sort wsel with cuobj mode fwprd wtsta.

loop at msel where idxwt eq index.

read table wsel with key cuobj eq msel-cuobj

and mode eq ymodes-mode

and fwprd ne 0

and wtsta ne 'D' binary search.

if sy-subrc eq 0.

ssel-fsum = ssel-fsum + wsel-fwprd.

endif.

endloop.

Use loop at where only when the number of records are less. While using loop at where each loop pass will search whole table for required condition so it will take large time.

Regards,

Sankar

Read only

RaymondGiuseppi
Active Contributor
0 Likes
850

Declare wsel as :

DATA WSEL TYPE TABLE OF xxx WHITH NON-UNIQUE KEY CUOBJ MODE.

and

  LOOP AT msel WHERE idxwt EQ index.
    LOOP AT wsel WHERE cuobj EQ msel-cuobj
                 AND mode EQ ymodes-mode.
      CHECK fwprd NE 0.
      CHECK  wtsta NE 'D'.
      ssel-fsum = ssel-fsum + wsel-fwprd.
      EXIT.
    ENDLOOP.
  ENDLOOP.

<i>When you use the WHERE condition with a STANDARD or HASHED table, a full table scan is always required.

If you are working with a SORTED TABLE, the partial sequential processing can be optimized so that only the entries that actually satisfy the WHERE condition are processed. This optimization requires that the WHERE condition be specified in the form

WHERE k1 = v1 AND k2 = v2 AND ... AND kn = vn

where the components k1, ..., kn are in the same sequence as the beginning of the table key.</i>

Regards

Read only

Former Member
0 Likes
850

hi

loop at msel where idxwt eq index.

***loop at wsel where cuobj eq msel-cuobj

read the table

if sy-surrc eq 0

ssel-fsum = ssel-fsum + wsel-fwprd.

endif.

exit

endloop.

reward if usefull