前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >两种删除internal table entry的性能比较

两种删除internal table entry的性能比较

作者头像
Jerry Wang
发布2019-07-03 11:21:06
6800
发布2019-07-03 11:21:06
举报

Created by Jerry Wang, last modified on May 07, 2014 Go to start of metadata

需求:internal table A和B 里分别包含了若干product ID,对于table A,如果其product ID 不在 table B里,则需要从table A中删除。 solution1: 遍历table A,对于每一行再分别到table B 中检测,如果line product ID在table B中不存在,则删除当前行 solution2: 直接使用ABAP keyword DELETE WHERE product_id NOT IN . 使用如下report 比较性能:

代码语言:javascript
复制
REPORT ZDELETE_COMPARE.

PARAMETERS: num type i OBLIGATORY DEFAULT 100.
types: begin of ty_product,
      id type comm_product-product_id,
      text type string,
     end of ty_product.

data: lt_product type STANDARD TABLE OF ty_product,
      lt_product1 LIKE lt_product,
      lt_compare type STANDARD TABLE OF comm_product-product_id,
      lt_range type RANGE OF comm_product-product_id,
      lv_start TYPE i,
      lv_end TYPE i.

FIELD-SYMBOLS: <product> TYPE ty_product,
               <range> LIKE LINE OF lt_range.

START-OF-SELECTION.
  PERFORM generate_main_tab.
  PERFORM solution1.
  PERFORM solution2.
  ASSERT lt_product = lt_product1.

FORM generate_main_tab.
   DO num TIMES.
     APPEND INITIAL LINE TO lt_product ASSIGNING <product>.
     <product>-id = sy-index.
     <product>-text = sy-index.
     APPEND INITIAL LINE TO lt_product1 ASSIGNING <product>.
     <product>-id = sy-index.
     <product>-text = sy-index.

     IF ( sy-index MOD 2 = 0 ).
        APPEND sy-index TO lt_compare.
     ENDIF.
   ENDDO.
ENDFORM.

FORM solution1.
   GET RUN TIME FIELD lv_start.

   LOOP AT lt_product ASSIGNING FIELD-SYMBOL(<product1>).
     READ TABLE lt_compare WITH KEY table_line = <product1>-id TRANSPORTING NO FIELDS.
     IF sy-subrc <> 0.
       DELETE TABLE lt_product FROM <product1>.
     ENDIF.
   ENDLOOP.

   GET RUN TIME FIELD lv_end.
   lv_end = lv_end - lv_start.
  WRITE: / 'Solution1: ' , lv_end COLOR COL_NEGATIVE.
ENDFORM.

FORM solution2.
   GET RUN TIME FIELD lv_start.

   LOOP AT lt_compare ASSIGNING FIELD-SYMBOL(<valid>).
      APPEND INITIAL LINE TO lt_range ASSIGNING <range>.
      <range>-low = <valid>.
      <range>-option = 'EQ'.
      <range>-sign = 'I'.
   ENDLOOP.

   DELETE lt_product1 WHERE id NOT IN lt_range.

   GET RUN TIME FIELD lv_end.
   lv_end = lv_end - lv_start.
  WRITE: / 'Solution2: ' , lv_end COLOR COL_NEGATIVE.
ENDFORM.

在internal table 的个数非常庞大的时候,直接使用ABAP keyword的solution具有更好的性能。

image
image
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年07月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Created by Jerry Wang, last modified on May 07, 2014 Go to start of metadata
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档