loop循环时须注意效率问题

系统 2211 0

以前写循环时,都是一条记录一条记录的比较,代码示例如下:

loop at S_VAL_CHAR into VAL_CHAR.
    if VAL_CHAR-CHARACT eq 'P0008_7'."箱型
        IT_ITEM-BOX = VAL_CHAR-VALUE_CHAR.
    ELSEIF VAL_CHAR-CHARACT eq 'S0012'."客户编号
        IT_ITEM-CLIENT_CODE = VAL_CHAR-VALUE_CHAR.
    ELSEIF VAL_CHAR-CHARACT eq 'CP_CW'."客户编号
        IT_ITEM-CP_CW = VAL_CHAR-VALUE_CHAR.
    ENDIF.
ENDLOOP.

这样的话要循环lines(  S_VAL_CHAR ).

应该改成如下,效率应该会好一点.

loop at S_VAL_CHAR into VAL_CHAR WHERE CHARACT eq 'P0008_7' OR CHARACT eq 'S0012' OR CHARACT eq 'CP_CW'.
    if VAL_CHAR-CHARACT eq 'P0008_7'."箱型
        IT_ITEM-BOX = VAL_CHAR-VALUE_CHAR.
    ELSEIF VAL_CHAR-CHARACT eq 'S0012'."客户编号
        IT_ITEM-CLIENT_CODE = VAL_CHAR-VALUE_CHAR.
    ELSEIF VAL_CHAR-CHARACT eq 'CP_CW'."客户编号
        IT_ITEM-CP_CW = VAL_CHAR-VALUE_CHAR.
    ENDIF.
ENDLOOP.

 若条件很多,条件语句写起来太长,可用range,代码如下:

data:lr_character type range of BAPI1003_ALLOC_VALUES_CHAR-CHARACT,
lr_character_line like line of lr_character.
CLEAR: lr_character_line,lr_character[].
lr_character_line = 'IEQ'.
lr_character_line-low = 'P0008_7'.
append lr_character_line to lr_character.

CLEAR: lr_character_line.
lr_character_line = 'IEQ'.
lr_character_line-low = 'S0012'.
append lr_character_line to lr_character.

CLEAR: lr_character_line.
lr_character_line = 'IEQ'.
lr_character_line-low = 'CP_CW'.
append lr_character_line to lr_character.

loop at S_VAL_CHAR into VAL_CHAR WHERE CHARACT in lr_character.
    if VAL_CHAR-CHARACT eq 'P0008_7'."箱型
        IT_ITEM-BOX = VAL_CHAR-VALUE_CHAR.
    ELSEIF VAL_CHAR-CHARACT eq 'S0012'."客户编号
        IT_ITEM-CLIENT_CODE = VAL_CHAR-VALUE_CHAR.
    ELSEIF VAL_CHAR-CHARACT eq 'CP_CW'."客户编号
        IT_ITEM-CP_CW = VAL_CHAR-VALUE_CHAR.
    ENDIF.
ENDLOOP.

 

继续优化一下,把那些重复的代码提取出来,最后如下:

data:    lr_character type range of BAPI1003_ALLOC_VALUES_CHAR-CHARACT,
            lr_character_line like line of lr_character.
            
form ADD_CHARACTER_TO_RANGE USING value(s) CHANGING r.
    clear lr_character_line.
    lr_character_line = 'IEQ'.
    lr_character_line-low = s.
    append lr_character_line to lr_character.
ENDFORM.

CLEAR: lr_character[].
perform ADD_CHARACTER_TO_RANGE using 'P0008_7' changing lr_character.
perform ADD_CHARACTER_TO_RANGE using 'S0012' changing lr_character.
perform ADD_CHARACTER_TO_RANGE using 'CP_CW' changing lr_character.

loop at S_VAL_CHAR into VAL_CHAR WHERE CHARACT in lr_character.
    if VAL_CHAR-CHARACT eq 'P0008_7'."箱型
        IT_ITEM-BOX = VAL_CHAR-VALUE_CHAR.
    ELSEIF VAL_CHAR-CHARACT eq 'S0012'."客户编号
        IT_ITEM-CLIENT_CODE = VAL_CHAR-VALUE_CHAR.
    ELSEIF VAL_CHAR-CHARACT eq 'CP_CW'."客户编号
        IT_ITEM-CP_CW = VAL_CHAR-VALUE_CHAR.
    ENDIF.
ENDLOOP. 

loop循环时须注意效率问题


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论