二维码

[性能] Reference variable、Workarea 和 Field-Symbols性能比较

Twilight发表于 2016-10-11 15:59Twilight 最后回复于 2016-10-11 15:59 [复制链接] 3248 0

在ABAP release 610版本之后,就支持REFERENCE INTO语法了,REFERENCE INTO常用在内表操作LOOP、READ语句中。

简单的case:
  1. DATA: t_t100 TYPE STANDARD TABLE OF t100.
  2. DATA: lr_t100 TYPE REF TO t100.                " << Reference variable

  3. SELECT *
  4.   FROM t100 INTO TABLE t_t100 UP TO 10 ROWS.

  5. LOOP AT t_t100 REFERENCE INTO lr_t100.         " << working with it
  6.   WRITE: / lr_t100->msgnr.                     " << Accessing the component
  7.   lr_t100->msgnr = '999'.

  8. ENDLOOP.

  9. LOOP AT t_t100 REFERENCE INTO lr_t100.
  10.   WRITE: / lr_t100->msgnr.
  11. ENDLOOP.
复制代码

Reference variable、Workarea 和 Field-Symbols的Performance测试案例:
  1. DATA: i_bseg TYPE STANDARD TABLE OF bseg,
  2.       wa_bseg LIKE LINE OF i_bseg.

  3. DATA: lv_flag TYPE flag,
  4.       lv_sta_time TYPE timestampl,
  5.       lv_end_time TYPE timestampl,
  6.       lv_diff_w   TYPE p DECIMALS 5,
  7.       lv_diff_f   LIKE lv_diff_w,
  8.       lv_diff_r   LIKE lv_diff_w,
  9.       lv_save     LIKE lv_diff_w.

  10. FIELD-SYMBOLS: <fs_bseg> LIKE LINE OF i_bseg.

  11. DATA: o_bseg TYPE REF TO bseg.

  12. * data selection = 100,000 records
  13. SELECT * FROM bseg INTO TABLE i_bseg UP TO 100000 ROWS.

  14. * Begin - Processing with Work area
  15. GET TIME STAMP FIELD lv_sta_time.
  16. LOOP AT i_bseg INTO wa_bseg.
  17.   IF lv_flag = 'X'.
  18.     wa_bseg-sgtxt = 'TEST'.
  19.     MODIFY i_bseg FROM wa_bseg.
  20.   ENDIF.
  21.   CLEAR wa_bseg.
  22.   IF lv_flag IS INITIAL.
  23.     lv_flag = 'X'.
  24.   ENDIF.
  25. ENDLOOP.
  26. GET TIME STAMP FIELD lv_end_time.
  27. lv_diff_w = lv_end_time - lv_sta_time.
  28. WRITE: /(30) 'Work area', lv_diff_w.
  29. * End   - Processing with Work Area

  30. CLEAR: lv_flag,
  31.        lv_sta_time,
  32.        lv_end_time.
  33. * Begin - Processing with Field-Symbols
  34. GET TIME STAMP FIELD lv_sta_time.
  35. LOOP AT i_bseg ASSIGNING <fs_bseg>.
  36.   IF lv_flag = 'X'.
  37.     <fs_bseg>-sgtxt = 'TEST'.
  38.   ENDIF.
  39.   IF lv_flag IS INITIAL.
  40.     lv_flag = 'X'.
  41.   ENDIF.
  42. ENDLOOP.
  43. GET TIME STAMP FIELD lv_end_time.
  44. lv_diff_f = lv_end_time - lv_sta_time.
  45. WRITE: /(30) 'Field-Symbol', lv_diff_f.
  46. * End   - Processing with FS

  47. * Net time saving
  48. lv_save = lv_diff_w - lv_diff_f.
  49. WRITE: /(30) 'Total Save (FS-WA)', lv_save.

  50. CLEAR: lv_flag,
  51.        lv_sta_time,
  52.        lv_end_time.
  53. * Begin - Processing with Field-Symbols
  54. GET TIME STAMP FIELD lv_sta_time.
  55. LOOP AT i_bseg REFERENCE INTO o_bseg.
  56.   IF lv_flag = 'X'.
  57.     o_bseg->sgtxt = 'TEST'.
  58.   ENDIF.
  59.   IF lv_flag IS INITIAL.
  60.     lv_flag = 'X'.
  61.   ENDIF.
  62. ENDLOOP.
  63. GET TIME STAMP FIELD lv_end_time.
  64. lv_diff_r = lv_end_time - lv_sta_time.
  65. WRITE: /(30) 'Reference', lv_diff_r.
  66. * End   - Processing with FS

  67. * Net time saving
  68. lv_save = lv_diff_f - lv_diff_r.
  69. WRITE: /(30) 'Total Save(Ref-FS)', lv_save.

  70. WRITE: / 'Done'.
复制代码

测试结果:
reference.jpg
由此可见Field-Symbols性能最高,其次是Reference variable,最差的是Workarea,建议编程是多使用Field-Symbols,如果你喜欢oop,建议使用Reference variable。
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表