SAP-ABAP-Code 测试排序表&哈希表的性能

*&---------------------------------------------------------------------*
*& Report YTEST0001
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YTEST0001.

TABLES:vbak,
       vbap.    "

************************************************************************

**  定义结构类型 Define the structure's type                          **

************************************************************************

TYPES:BEGIN OF ty_result,
        vbeln  TYPE sy-tabix,
        posnr  TYPE vbap-posnr,
        kunnr  TYPE vbak-kunnr,
        name1  TYPE kna1-name1,
        matnr  TYPE vbap-matnr,
        arktx  TYPE vbap-arktx,
        kwmeng TYPE vbap-kwmeng,
        meins  TYPE vbap-meins,
        kbetr  TYPE konv-kbetr,
        netwr  TYPE vbap-netwr,
        waerk  TYPE vbap-waerk,
      END OF ty_result.

************************************************************************

**  定义变量与内表 Define the variants and Internal tables            **

************************************************************************

DATA:gt_hash   TYPE HASHED TABLE OF ty_result WITH UNIQUE KEY vbeln posnr,
     gt_sort   TYPE SORTED TABLE OF ty_result WITH UNIQUE KEY vbeln posnr,
     gs_result TYPE ty_result,
     gv_rows   TYPE sy-tabix.


************************************************************************
**  宏定义 Define the macro                                           **
************************************************************************

DEFINE mcr_range.

  clear &1.
  &1-sign = 'I'.
  &1-option = &2.
  &1-low = &3.
  &1-high = &4.
  append &1.

END-OF-DEFINITION.

************************************************************************

**  选择屏幕 Customize the selection-screen                           **

************************************************************************

SELECTION-SCREEN BEGIN OF BLOCK xavery WITH FRAME TITLE text-001.
PARAMETERS:p_rows TYPE sytabix DEFAULT '100',     "初始化内表的记录数
           p_loop TYPE sytabix DEFAULT '100'.     "读取的次数
SELECTION-SCREEN END OF BLOCK xavery.

************************************************************************
**  执行程序事件 Executing the program's events                       **
************************************************************************

INITIALIZATION.

START-OF-SELECTION.

  PERFORM sub_init_data.
  PERFORM sub_sort_table.
  PERFORM sub_hash_table.

END-OF-SELECTION.

*@---------------------------------------------------------------------*
*@      Form  SUB_INIT_DATA
*@---------------------------------------------------------------------*
*       初始化选择条件
*----------------------------------------------------------------------*

FORM sub_init_data .

  DEFINE mcr_init.

    clear gs_result.
    gs_result-vbeln = sy-index.
    gs_result-kunnr = '100000000'.
    gs_result-matnr = 'A0010312'.

    do 10 times.
      gs_result-posnr = gs_result-posnr + 10.
      insert gs_result into table gt_hash.
      insert gs_result into table gt_sort.
    enddo.

  END-OF-DEFINITION.

  REFRESH:gt_hash,
          gt_sort.

  DO p_rows TIMES.
    mcr_init.
  ENDDO.

ENDFORM.                    " SUB_INIT_DATA

*&---------------------------------------------------------------------*
*&      Form  SUB_HASH_TABLE
*&---------------------------------------------------------------------*
*       测试哈希表的性能
*----------------------------------------------------------------------*

FORM sub_hash_table .

  CLEAR gv_rows.

  DO p_loop TIMES.
    CLEAR gs_result.
    READ TABLE gt_hash INTO gs_result
                       WITH KEY vbeln = sy-index
                                posnr = '000020'.
*                                TRANSPORTING NO FIELDS.

    IF sy-subrc = 0.
      gv_rows = gv_rows + 1.
    ENDIF.

  ENDDO.

  WRITE:/ gv_rows.

ENDFORM.                    " SUB_HASH_TABLE

*&---------------------------------------------------------------------*

*&      Form  SUB_SORT_TABLE

*&---------------------------------------------------------------------*

*       测试排序表的性能

*----------------------------------------------------------------------*

FORM sub_sort_table .

  CLEAR gv_rows.

  DO p_loop TIMES.
    CLEAR gs_result.
    READ TABLE gt_sort INTO gs_result
                       WITH KEY vbeln = sy-index
                                posnr = '000020'
                                BINARY SEARCH.

*                                TRANSPORTING NO FIELDS.

    IF sy-subrc = 0.
      gv_rows = gv_rows + 1.
    ENDIF.

  ENDDO.

  WRITE:/ gv_rows.

ENDFORM.                    " SUB_SORT_TABLE

 

posted on 2021-11-26 10:00  Violin_Huang  阅读(146)  评论(0编辑  收藏  举报

导航