ABAP Workarea(工作区) VS Field Symbols(占位符) 性能对比分析

在SAP开发项目,这两个概念开发人员基本都知道。但是没有去对比其性能区别。
首先,我们普及一下原理。
Workarea: 相当于在内存堆栈里面开辟了一块内存。
Field Symbols:相当于存了内存的地址。所以实际上是不占用内存的。相当于C语言的已经被解除引用的指针。
所以我们进行一项性能测试:
1.对比单纯Loop的性能,不进行数据修改。

REPORT zloop_performance_test.


DATA : v_start_time TYPE timestampl,
       v_end_time   TYPE timestampl,
       v_diff_w     TYPE p DECIMALS 5.


TYPES : BEGIN OF ty_vbrp,
          vbeln TYPE vbrp-vbeln,
        END OF ty_vbrp.


DATA : lt_vbrp TYPE STANDARD TABLE OF ty_vbrp,
       ls_vbrp TYPE ty_vbrp.


FIELD-SYMBOLS :  TYPE ty_vbrp.


PARAMETERS p_count TYPE n LENGTH 6.


* Start of selection
START-OF-SELECTION.
* Get billing item data
  SELECT vbeln
      FROM vbap
      INTO TABLE lt_vbrp
      UP TO p_count ROWS.


END-OF-SELECTION.


* 1 . Loop with work area


  GET TIME STAMP FIELD v_start_time.


  LOOP AT lt_vbrp INTO ls_vbrp.
  ENDLOOP.


  GET TIME STAMP FIELD v_end_time.
  v_diff_w = v_end_time - v_start_time.


  WRITE :/(30) 'Loop with Work area', v_diff_w.
  CLEAR : v_start_time, v_end_time, v_end_time.
* 2 . Loop with work area inline declaration
  GET TIME STAMP FIELD v_start_time.


  LOOP AT lt_vbrp INTO DATA(ls_vbrp_inline).
  ENDLOOP.


  GET TIME STAMP FIELD v_end_time.


  v_diff_w = v_end_time - v_start_time.


  WRITE:/(30) 'Loop with Inline Work area', v_diff_w.
  CLEAR : v_start_time, v_end_time, v_end_time.
*   3 . Loop with Field symbol
  GET TIME STAMP FIELD v_start_time.


  LOOP AT lt_vbrp ASSIGNING .
  ENDLOOP.


  GET TIME STAMP FIELD v_end_time.


  v_diff_w = v_end_time - v_start_time.


  WRITE:/(30) 'Loop with Field Symbol', v_diff_w.
  CLEAR : v_start_time, v_end_time, v_end_time.
* 4 . Loop with Field symbol inline declaration


  GET TIME STAMP FIELD v_start_time.


  LOOP AT lt_vbrp ASSIGNING FIELD-SYMBOL().
  ENDLOOP.


  GET TIME STAMP FIELD v_end_time.


  v_diff_w = v_end_time - v_start_time.

  WRITE:/(30) 'Loop with Inline Field Symbol', v_diff_w.
  CLEAR : v_start_time, v_end_time, v_end_time.

ABAP Workarea(工作区) VS Field Symbols(占位符) 性能对比分析_第1张图片

2.对比loop,并将数据修改如下面的代码:

REPORT zloop_performance_test.


DATA : v_start_time TYPE timestampl,
       v_end_time   TYPE timestampl,
       v_diff_w     TYPE p DECIMALS 5.


TYPES : BEGIN OF ty_vbrp,
          vbeln TYPE vbrp-vbeln,
        END OF ty_vbrp.


DATA : lt_vbrp TYPE STANDARD TABLE OF ty_vbrp,
       ls_vbrp TYPE ty_vbrp.


FIELD-SYMBOLS :  TYPE ty_vbrp.


PARAMETERS p_count TYPE n LENGTH 6.


* Start of selection


START-OF-SELECTION.


* Get billing item data
  SELECT  vbeln
      FROM vbrp
      INTO TABLE lt_vbrp
      UP TO p_count ROWS.


END-OF-SELECTION.


* 1 . Loop with work area


  GET TIME STAMP FIELD v_start_time.


  LOOP AT lt_vbrp INTO ls_vbrp.
    ls_vbrp-vbeln = 1.
    MODIFY  lt_vbrp FROM ls_vbrp.
  ENDLOOP.


  GET TIME STAMP FIELD v_end_time.
  v_diff_w = v_end_time - v_start_time.


  WRITE :/(30) 'Loop with Work area', v_diff_w.
  CLEAR : v_start_time, v_end_time, v_end_time.
* 2 . Loop with work area inline declaration
  GET TIME STAMP FIELD v_start_time.


  LOOP AT lt_vbrp INTO DATA(ls_vbrp_inline).
    ls_vbrp_inline-vbeln = 1.
    MODIFY  lt_vbrp FROM ls_vbrp_inline.
  ENDLOOP.


  GET TIME STAMP FIELD v_end_time.


  v_diff_w = v_end_time - v_start_time.


  WRITE:/(30) 'Loop with Inline Work area', v_diff_w.
  CLEAR : v_start_time, v_end_time, v_end_time.
*   3 . Loop with Field symbol
  GET TIME STAMP FIELD v_start_time.


  LOOP AT lt_vbrp ASSIGNING .
    -vbeln = 1.
  ENDLOOP.


  GET TIME STAMP FIELD v_end_time.


  v_diff_w = v_end_time - v_start_time.


  WRITE:/(30) 'Loop with Field Symbol', v_diff_w.
  CLEAR : v_start_time, v_end_time, v_end_time.
* 4 . Loop with Field symbol inline declaration


  GET TIME STAMP FIELD v_start_time.


  LOOP AT lt_vbrp ASSIGNING FIELD-SYMBOL().
    -vbeln = 1.
  ENDLOOP.


  GET TIME STAMP FIELD v_end_time.


  v_diff_w = v_end_time - v_start_time.


  WRITE:/(30) 'Loop with Inline Field Symbol', v_diff_w.
  CLEAR : v_start_time, v_end_time, v_end_time.

ABAP Workarea(工作区) VS Field Symbols(占位符) 性能对比分析_第2张图片

结论:

在两个测试结果中可以看出,loop到工作区实际上有一个内表到工作区的赋值过程。从工作区modify到内表也有一个赋值的过程。而在Field Symbol中则是直接修改了内表值。所以速度会比较快很多。

所以在能用Field Symbol的地方尽量使用Field Symbol。

你可能感兴趣的:(abap性能优化sap)