Use of REFERENCE variable vs Workarea vs Field-Symbols

By | March 13, 2014 | Performance | 10,271 | 4

Along with ASSIGNING addition for the itab processing, we have REFERENCE INTO. Let’s take a deep dive into its usage.

REFERENCE INTO

Since ABAP release 610, we can specify the REFERENCE INTO with Internal table operations like LOOP, READ etc. In order to use the reference variable, you need to define the variable using REF TO. The variable than be declared as the data reference object. Since you define the variable with specific type, it has all the components of that type. Once the variable is referenced, you can then use the operator -> to access the individual components, same as you try to access any attribute of an object.

Sample code to work with REFERENCE INTO

 
DATA: t_t100 TYPE STANDARD TABLE OF t100.
DATA: lr_t100 TYPE REF TO t100.                " << Reference variable
 
SELECT * 
  FROM t100 INTO TABLE t_t100 UP TO 10 ROWS.
 
LOOP AT t_t100 REFERENCE INTO lr_t100.         " << working with it
  WRITE: / lr_t100->msgnr.                     " << Accessing the component
  lr_t100->msgnr = '999'.
 
ENDLOOP.
 
LOOP AT t_t100 REFERENCE INTO lr_t100.
  WRITE: / lr_t100->msgnr.
ENDLOOP.
 
 

Performance Comparison

Since you can also access the itab records using Workarea and Field-Symobls, I thought of extending the earlier program used in the article Use of Field-Symbol vs WA to include the performance measurement of working with the Reference variable as well.

Demo Program - Performance Comparison

 
REPORT  ztest_np_loop_reference.
 
*
DATA: i_bseg TYPE STANDARD TABLE OF bseg,
      wa_bseg LIKE LINE OF i_bseg.
*
DATA: lv_flag TYPE flag,
      lv_sta_time TYPE timestampl,
      lv_end_time TYPE timestampl,
      lv_diff_w   TYPE p DECIMALS 5,
      lv_diff_f   LIKE lv_diff_w,
      lv_diff_r   LIKE lv_diff_w,
      lv_save     LIKE lv_diff_w.
*
FIELD-SYMBOLS: <fs_bseg> LIKE LINE OF i_bseg.
 
data: o_bseg type REF TO bseg.
*
* data selection = 10,000 records
SELECT * FROM bseg INTO TABLE i_bseg UP TO 100 ROWS.
*
* Begin - Processing with Work area
GET TIME STAMP FIELD lv_sta_time.
LOOP AT i_bseg INTO wa_bseg.
  IF lv_flag = 'X'.
    wa_bseg-sgtxt = 'TEST'.
    MODIFY i_bseg FROM wa_bseg.
  ENDIF.
  CLEAR wa_bseg.
  IF lv_flag IS INITIAL.
    lv_flag = 'X'.
  ENDIF.
ENDLOOP.
GET TIME STAMP FIELD lv_end_time.
lv_diff_w = lv_end_time - lv_sta_time.
WRITE: /(30) 'Work area', lv_diff_w.
* End   - Processing with Work Area
*
CLEAR: lv_flag,
       lv_sta_time,
       lv_end_time.
* Begin - Processing with Field-Symbols
GET TIME STAMP FIELD lv_sta_time.
LOOP AT i_bseg ASSIGNING <fs_bseg>.
  IF lv_flag = 'X'.
    <fs_bseg>-sgtxt = 'TEST'.
  ENDIF.
  IF lv_flag IS INITIAL.
    lv_flag = 'X'.
  ENDIF.
ENDLOOP.
GET TIME STAMP FIELD lv_end_time.
lv_diff_f = lv_end_time - lv_sta_time.
WRITE: /(30) 'Field-Symbol', lv_diff_f.
* End   - Processing with FS
*
* Net time saving
lv_save = lv_diff_w - lv_diff_f.
WRITE: /(30) 'Total Save (FS-WA)', lv_save.
*
CLEAR: lv_flag,
       lv_sta_time,
       lv_end_time.
* Begin - Processing with Field-Symbols
GET TIME STAMP FIELD lv_sta_time.
LOOP AT i_bseg REFERENCE INTO o_bseg.
  IF lv_flag = 'X'.
    o_bseg->sgtxt = 'TEST'.
  ENDIF.
  IF lv_flag IS INITIAL.
    lv_flag = 'X'.
  ENDIF.
ENDLOOP.
GET TIME STAMP FIELD lv_end_time.
lv_diff_r = lv_end_time - lv_sta_time.
WRITE: /(30) 'Reference', lv_diff_r.
* End   - Processing with FS
*
* Net time saving
lv_save = lv_diff_f - lv_diff_r.
WRITE: /(30) 'Total Save(Ref-FS)', lv_save.
 
WRITE: / 'Done'.
 

Average Results on Graph:


Verdict

Field-symbols gives highest performance improvement compared to Workarea and Reference variable. Field-Symbols is still a winner of the race, when there more data. So, if you are using FS, keep using it. If you want to use more object oriented like data access, use the REFERENCE INTO.

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{272 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 272 articles.

Load comments

4 Comments

  • Steve Oldner

    This is where the cool people learn!

    Thanks for the performance compare. Now I need to try this myself!

  • Himansu Gyala

    I always try to use field symbols in my programs, but REFERENCE INTO is a new for me as I never used in my program. Like your this post too :)

    Regards,
    Himansu

  • Kris @ How ABAP

    Hai Naimesh thank you for this code benchmark test, I learn so much from this tutorial. I think the reason why field symbol is faster is because it doesn’t have to use modify to update afield, such as modify itab from ld_itab, whenever we assign a new value into -col then it will automatically update the field.

  • on internal table looping, reference into is only for ppl who likes OO fetish.
    field symobol is deferenced version of reference. To use reference on displaying, reading,writng you need to derefere (which means converting to field symbol).
    So be a good boy and use field symbol.

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.