Loop & Loop

小小抱怨一下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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值