Best way to Loop At ‘Standard’ Internal Table in ABAP

2
747

When it comes to looping thru a standard Internal Table, almost every ABAPer normally loops like this (I_COEP is already sorted)
(1) Loop at I_COEP where kokrs = i_cobk-kokrs
and belnr = i_cobk-belnr.
……
endloop.

And this the SLOWEST method to be adopted.

The next best method is accessing the right set of data, using Binary Search, and then read thru it.

(2) read table i_coep with key
kokrs = i_cobk-kokrs
belnr = i_cobk-belnr
binary search.
if sy-subrc = 0.
Loop at i_coep from sy-tabix
where kokrs = i_cobk-kokrs
and belnr = i_cobk-belnr.
…….
Endloop.
endif.

But ‘the best of the lot’ goes to the code, which is the extension of second method defined above. This code exits when no matching criteria is found and thus avoid looping thru the whole internal table.

(3) read table i_coep with key
kokrs = i_cobk-kokrs
belnr = i_cobk-belnr
binary search.
if sy-subrc = 0.
Loop at i_coep from sy-tabix.
if ( i_coep-kokrs NE i_cobk-kokrs or
i_coep-belnr NE i_cobk-belnr )
exit.
endif.
Endloop.
endif.

2 COMMENTS

  1. It was an amazing technique ,i am using it from last two years and it works great ,if you are using nested loops this is the best technique to make your speed faster.

LEAVE A REPLY

Please enter your comment!
Please enter your name here