ABAP Mesh Performance Test

To see if internal table access via Mesh is faster / slower than the traditional method, I decided to make a simple performance test. Here is the source code:

REPORT ztest.

PARAMETERS: p_matnr TYPE matnr OBLIGATORY,
            p_meins TYPE meins OBLIGATORY.

CLASS main DEFINITION.
  PUBLIC SECTION.

    TYPES: BEGIN OF t_material,
             matnr TYPE mara-matnr,
             maktx TYPE makt-maktx,
           END OF t_material,

           tt_material TYPE HASHED TABLE OF t_material WITH UNIQUE KEY primary_key COMPONENTS matnr,

           BEGIN OF t_uom,
             matnr TYPE marm-matnr,
             meinh TYPE marm-meinh,
             umrez TYPE marm-umrez, " Dummy
           END OF t_uom,

           tt_uom TYPE HASHED TABLE OF t_uom
                  WITH UNIQUE KEY primary_key COMPONENTS matnr meinh
                  with non-unique sorted key k1 components matnr,

           BEGIN OF MESH t_material_master,
             materials TYPE tt_material ASSOCIATION mat_uoms TO uoms      ON matnr = matnr using key k1,
             uoms      TYPE tt_uom      ASSOCIATION uom_mats TO materials ON matnr = matnr,
           END OF MESH t_material_master.

    CLASS-DATA gs_master TYPE t_material_master.

    CLASS-METHODS:
      demo,
      mesh,
      old.

ENDCLASS.

CLASS main IMPLEMENTATION.
  METHOD demo.

    " Select data """""""""""""""""""""""""""""""""""""""""

    SELECT * FROM mara INTO CORRESPONDING FIELDS OF TABLE @gs_master-materials.
    SELECT * FROM marm INTO CORRESPONDING FIELDS OF TABLE @gs_master-uoms.

    " MESH """""""""""""""""""""""""""""""""""""""""""""""""""""

    get time.
    data(lv_mesh_start) = sy-timlo.
    do 1000 times.
      mesh( ).
    enddo.

    get time.
    data(lv_mesh_end) = sy-timlo.
    data(lv_mesh_Diff) = lv_mesh_end - lv_mesh_start.
    new-line.
    write |Mesh performance: { lv_mesh_diff }|.

    " Old """"""""""""""""""""""""""""""""""""""""""""""""""""""

    get time.
    data(lv_old_start) = sy-timlo.
    do 1000 times.
      old( ).
    enddo.

    get time.
    data(lv_old_end) = sy-timlo.
    data(lv_old_Diff) = lv_old_end - lv_old_start.
    new-line.
    write |Old performance: { lv_old_diff }|.

  ENDMETHOD.

  method mesh.

    " Mat to UOM """"""""""

    ASSIGN gs_master-materials[ KEY primary_key COMPONENTS matnr = p_matnr ] TO FIELD-SYMBOL(<ls_material>).

    LOOP AT gs_master-materials\mat_uoms[ <ls_material> ] ASSIGNING FIELD-SYMBOL(<ls_mat_uom>).
      "NEW-LINE.
      "WRITE |Material { <ls_material>-matnr } UOM { <ls_mat_uom>-meinh }|.
    ENDLOOP.

    " UOM to MAT """"""""""

    LOOP AT gs_master-uoms ASSIGNING <ls_mat_uom> WHERE meinh EQ p_meins.
      assign gs_master-uoms\uom_mats[ <ls_mat_uom> ] to <ls_material>.
    ENDLOOP.

  endmethod.

  method old.

    " Mat to UOM """"""""""

    ASSIGN gs_master-materials[ KEY primary_key COMPONENTS matnr = p_matnr ] TO FIELD-SYMBOL(<ls_material>).

    loop at gs_master-uoms assigning field-symbol(<ls_mat_uom>)
            using key k1
            where matnr eq <ls_material>-matnr.
    endloop.

    " UOM to MAT """"""""""

    LOOP AT gs_master-uoms ASSIGNING <ls_mat_uom> WHERE meinh EQ p_meins.
      assign gs_master-materials[ key primary_key components matnr = <ls_mat_uom>-matnr ] to <ls_material>.
    ENDLOOP.

  endmethod.

ENDCLASS.

START-OF-SELECTION.

  main=>demo( ).

Result was a tie:

That leaves virtually no reason not to use Mesh.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: