Fork me on GitHub

ABAP 内表访问表达式的性能

 

内表访问表达式是ABAP 7.4中引入的重要特性,可以使语句变得更加简洁、美观。那么它的读写性能怎么样呢?我进行了一点点测试。

 

读取

测试代码,使用三种方式读取同一内表,分别是read table关键字、表达式和read table...binary search:

REPORT ztest_read.


DATA: BEGIN OF gs_test,
        index TYPE i,
        des   TYPE string,
      END OF gs_test.
DATA: gt_test LIKE STANDARD TABLE OF gs_test.


CLASS zcl_reader DEFINITION.

  PUBLIC SECTION.

    METHODS:
      constructor IMPORTING itab LIKE gt_test,
      read_by_read,
      read_by_exp,
      read_by_binary.

  PRIVATE SECTION.

    DATA: lt_test  LIKE STANDARD TABLE OF gs_test.

   CLASS-DATA index1 TYPE i VALUE 3769.    CLASS-DATA index2 TYPE i VALUE 5555.
ENDCLASS.
CLASS zcl_reader IMPLEMENTATION. METHOD constructor. lt_test = itab. ENDMETHOD. METHOD read_by_read. READ TABLE lt_test WITH KEY index = index1 INTO DATA(ls_test). READ TABLE lt_test WITH KEY index = index2 INTO ls_test. ENDMETHOD. METHOD read_by_exp. DATA(ls_test) = lt_test[ index = index1 ]. ls_test = lt_test[ index = index2 ]. ENDMETHOD. METHOD read_by_binary. READ TABLE lt_test WITH KEY index = index1 INTO DATA(ls_test) BINARY SEARCH. READ TABLE lt_test WITH KEY index = index2 INTO ls_test BINARY SEARCH. ENDMETHOD. ENDCLASS. INITIALIZATION. DATA(des) = '一个字符串'. DO 30000 TIMES. gs_test-index = gs_test-index + 1. gs_test-des = des. APPEND gs_test TO gt_test. ENDDO. DATA(gr_reader) = NEW zcl_reader( gt_test ). gr_reader->read_by_read( ). gr_reader->read_by_exp( ). gr_reader->read_by_binary( ).

结果:

 

如图,内表访问表达式在读取方面的性能和read table是一样的。所以,表达式在这里只是read table的简写方式。

修改

测试代码,分别使用modify关键字,内表访问表达式,field symbol来修改内表:

*&---------------------------------------------------------------------*
*& Report  ZTEST_ITAB_MODIFY
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ztest_itab_modify.


DATA: BEGIN OF gs_test,
        index TYPE i,
        des   TYPE string,
      END OF gs_test.
DATA: gt_test LIKE STANDARD TABLE OF gs_test.


CLASS zcl_editor DEFINITION.


  PUBLIC SECTION.

    METHODS:
      constructor IMPORTING itab LIKE gt_test,

      modify_by_modify,

      modify_by_exp,

      modify_by_fs.

  PRIVATE SECTION.

    CLASS-DATA new_string TYPE string VALUE '喵喵喵?'.

    DATA: lt_test1  LIKE STANDARD TABLE OF gs_test.
    DATA: lt_test2  LIKE STANDARD TABLE OF gs_test.
    DATA: lt_test3  LIKE STANDARD TABLE OF gs_test.

ENDCLASS.

CLASS zcl_editor IMPLEMENTATION. METHOD constructor. lt_test1 = itab. lt_test2 = itab. lt_test3 = itab. ENDMETHOD. METHOD modify_by_modify. DATA idx TYPE i VALUE 1. DO lines( lt_test1 ) TIMES. READ TABLE lt_test1 INTO DATA(ls_test) INDEX idx . ls_test-des = new_string. MODIFY lt_test1 FROM ls_test INDEX idx. idx = idx + 1. ENDDO. ENDMETHOD. METHOD modify_by_exp. DATA idx TYPE i VALUE 1. DO lines( lt_test2 ) TIMES. lt_test2[ idx ]-des = new_string. idx = idx + 1. ENDDO. ENDMETHOD. METHOD modify_by_fs. DATA idx TYPE i VALUE 1. FIELD-SYMBOLS: <fs> LIKE gs_test. DO lines( lt_test3 ) TIMES. READ TABLE lt_test1 ASSIGNING <fs> INDEX idx . <fs>-des = new_string. idx = idx + 1. ENDDO. ENDMETHOD. ENDCLASS.
INITIALIZATION. DATA(des) = '一个字符串'. DO 30000 TIMES. gs_test-index = gs_test-index + 1. gs_test-des = des. APPEND gs_test TO gt_test. ENDDO. DATA(lr_editor) = NEW zcl_editor( gt_test ). lr_editor->modify_by_modify( ). lr_editor->modify_by_exp( ). lr_editor->modify_by_fs( ).

 

结果:

 

可以看出,内表访问表达式在修改普通的标准表时的性能是要稍差于modify关键字的。更不如field symbol。

当然,本文中使用的例子不能代表实际业务场景,因此只可以用作参考。

posted @ 2017-06-24 16:13  氢氦  阅读(2032)  评论(0编辑  收藏  举报