小小抱怨一下blog,之前的文章发重复了,找了半天竟然找不到删除按键!!!让人很无语。
两层的loop优化,之前一直都有留意,今天重新发掘出来,以下摘抄自网上:
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.
sy-tabix是internal table的index。sy-index在循环里面,如do...enddo.