Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

need to optimize loop

Former Member
0 Kudos

i have to optimize colored line any one heaving idea? Please rply ..

SELECT * FROM zsng2g_pkt_eh

  INTO TABLE gt_pkt_eh

  WHERE ZSNG2G_ENC_TYPE  = gv_par_val_enc

  AND   ZSNG2G_EPC_CLASS = gv_par_val_epc

  AND   ZSNG2G_PACKET_LEVEL = '03'

  AND   ZSNG2G_REQUESTID = gv_id.

**-- selecting all the sn for give packet type list

  SELECT eh_guid ZSNG2G_SN_SERIAL_NUMBER ZSNG2G_SN_COUNTER FROM zsng2g_sn_eh

  INTO TABLE lt_sn_eh

  WHERE ZSNG2G_SN_ENCODING_TYPE = gv_par_val_epc

  AND   ZSNG2G_SN_EPCCLASS = gv_par_val_enc.

*    AND   ZSNG2G_SN_COUNTER ge gt_pkt_ehx-ZSNG2G_RANGE_START

*    AND   ZSNG2G_SN_COUNTER le gt_pkt_ehx-ZSNG2G_RANGE_END.

*--Filling tbl_range

  LOOP AT gt_pkt_eh into ls_pkt_eh.                   "where ZSNG2G_PACKET_TYPE = 'RANGE'.

    if ls_pkt_eh-ZSNG2G_PACKET_TYPE = 'RANGE'.

      ls_RANGE-RANGE_FROM = ls_pkt_eh-ZSNG2G_RANGE_START.

      ls_RANGE-RANGE_TO = ls_pkt_eh-ZSNG2G_RANGE_END.

      APPEND ls_range to lt_range.

    ELSEif ls_pkt_eh-ZSNG2G_PACKET_TYPE = 'LIST' .

*--Fill tbl_list

      LOOP AT lt_sn_eh into ls_sn_eh where ZSNG2G_SN_COUNTER ge ls_pkt_eh-ZSNG2G_RANGE_START and

                                           ZSNG2G_SN_COUNTER le ls_pkt_eh-ZSNG2G_RANGE_END.

        ls_list-TABLE_LIST = ls_sn_eh-ZSNG2G_SN_SERIAL_NUMBER.

        APPEND ls_list to lt_list.

        clear ls_list.

      ENDLOOP.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Manisha,

I just modified the colored code, just try this in your system and see any performance

1. Declare one more field flag type c in  lt_sn_eh

2. Declare internal table lt_sn_eh as i mentioned


data lt_sn_eh type TABLE OF xxxxx with NON-UNIQUE SORTED KEY  sn_counter COMPONENTS zsng2g_sn_counter.

*--Filling tbl_range


ls_sn_eh-flag = abap_true. " to make required entries

LOOP AT gt_pkt_eh INTO ls_pkt_eh.                   "where ZSNG2G_PACKET_TYPE = 'RANGE'.

   IF ls_pkt_eh-zsng2g_packet_type = 'RANGE'.

     ls_range-range_from = ls_pkt_eh-zsng2g_range_start.

     ls_range-range_to     = ls_pkt_eh-zsng2g_range_end.

     APPEND ls_range TO lt_range.

   ELSEIF ls_pkt_eh-zsng2g_packet_type = 'LIST' .

*--Fill tbl_list

     MODIFY lt_sn_eh FROM ls_sn_eh USING KEY sn_counter TRANSPORTING flag  WHERE zsng2g_sn_counter GE ls_pkt_eh-zsng2g_range_start AND

   zsng2g_sn_counter LE ls_pkt_eh-zsng2g_range_end.

   ENDIF.

ENDLOOP.

LOOP AT lt_sn_eh ASSIGNING <fs_sn_eh> USING KEY sn_counter.

  if <fs_sn_eh>-flag = abap_true.

   ls_list-table_list = ls_sn_eh-zsng2g_sn_serial_number.

   APPEND ls_list TO lt_list.

   CLEAR ls_list.

endif.

ENDLOOP.

26 REPLIES 26

former_member216168
Active Participant
0 Kudos

Hi,

Try to use parallel cursor technique:

Parallel Cursor - 2: without using READ | ABAP Help Blog

0 Kudos

Another tip... try to use this development called Code Lighter. It helps a lot to understand the code when you post in SDN:

0 Kudos

in second loop it has condition.in parallel cursor method how to implement this becaz read is not possible with le and ge operator

paul_bakker2
Active Contributor
0 Kudos

Hi,

That coloured piece of code just loops at one internal table and fills another.

I can't imagine that it would have any significant effect on performance.

I think you're focusing on the wrong ABAP statements. Have you done a performance trace?

cheers

Paul

0 Kudos

in second loop it has condition.in parallel cursor method how to implement this becaz read is not possible with le and ge operator

0 Kudos

Like I said, that's not where your performance problem is.

cheers

Paul

VenkatRamesh_V
Active Contributor
0 Kudos

Hi Manisha,

Try this.

Instead of work area declare field symbols.


Field symbols: <fs> like line of  gt_pkt_eh.


LOOP AT gt_pkt_eh ASSIGNING <fs>.

CHECK  ( <fs>-PACKET_TYPE = 'RANGE'  OR

               <fs>-PACKET_TYPE = 'LIST' ).                           


ENDLOOP.



Regards,

Venkat.

Former Member
0 Kudos

As per

SAP Documentation

CASE statements are clearer and a little faster than

IF-constructions.

Regards,

Avirat

0 Kudos

Avirat Patel wrote:

As per SAP Documentation

CASE statements are clearer and a little faster than

IF-constructions.

Which SAP documentation?

"Clearer", agreed. "Faster", don't agree unless someone posts detailed runtime analysis.

0 Kudos

Dear Suhas I got this details from following screen.

0 Kudos

What does the "Measure runtime" tell you? Do you see any appreciable difference in runtimes?

0 Kudos

May be it works well where huge amount of data is available. and if It does not then why SAP has mentioned this note [Raising eyebrow].

if you know [you are a gem and i'm following you/your post always] kindly tell me .

0 Kudos

That's the age-old SE80 performance examples...my results:

IF: 0 microseconds

CASE: 0 microseconds

It seems that CPU-Power has increased somewhat since the tests were designed (15 years ago?)

Thomas

0 Kudos

It means that SAP has not upgrade its old age documentation?

0 Kudos

That's the age-old SE80 performance examples...my results:

They are still available on the latest runtime analysis tool - SAT

0 Kudos

Avirat Patel wrote:

It means that SAP has not upgrade its old age documentation?

Yeah! SAP & documentation, that's been on every ABAPer wishlist ad infinitum.

0 Kudos

In that case we have to strongly follow gurus (Suhas & Thomas Z)

and getting valuable tips & tricks

0 Kudos

Correct, there is a few examples for this. Doesn't make it easier to combat some of the "urban legends"...

In general, I would always look for significant differences when comparing runtimes and adjusting code (especially when code becomes more complex this way). Definition of "significant" might vary based on circumstances, but a few percent is not worth any effort in most cases.

The biggest performance hazards in my experience is still access to large data (DB or internal table) without index support, even more so when done in loops, and excessive memory usage (paging to disk).


Thomas

0 Kudos

Avirat Patel wrote:

May be it works well where huge amount of data is available. and if It does not then why SAP has mentioned this note [Raising eyebrow].

What do you mean by huge amount of data in an IF...ENDIF or CASE..ENDCASE block?

I don't know why SAP has mentioned this note. It's also mentioned in the - ABAP Keyword Documentation. But i have never seen a piece of code being slower because of IF-blocks. Maybe they contribute a few additional microseconds in the "System" runtime, but i wouldn't mind correcting those.

0 Kudos

Even better, follow your own observations, but make sure your test setups are correct.

Thomas

0 Kudos

My result: Case is little faster more often. It won't be called a significant difference though.

  1. TYPES:
  2. BEGIN OF ty_result,
  3.   case    TYPE timestamp,
  4.   ifelse  TYPE timestamp,
  5.   faster  TYPE char10,
  6.   percent TYPE decfloat34,
  7. END OF ty_result.
  8. DATA: lt_result TYPE STANDARD TABLE OF ty_result,
  9.       ls_result TYPE ty_result,
  10.       t1 TYPE timestamp,
  11.       t2 TYPE timestamp,
  12.       t3 TYPE timestamp.
  13. DO 100 TIMES.
  14.   CLEAR: t1, t2, t3.
  15.   GET RUN TIME FIELD t1.
  16.   DO 10000 TIMES.
  17.     CASE sy-index.
  18.       WHEN 1.
  19.       WHEN 2.
  20.       WHEN 3.
  21.       WHEN 4.
  22.       WHEN 5.
  23.       WHEN 6.
  24.       WHEN 7.
  25.       WHEN 8.
  26.       WHEN 9.
  27.       WHEN 10.
  28.       WHEN 11.
  29.       WHEN 12.
  30.       WHEN 13.
  31.       WHEN 14.
  32.       WHEN 15.
  33.       WHEN 16.
  34.       WHEN 17.
  35.       WHEN 18.
  36.       WHEN 19.
  37.       WHEN 20.
  38.       WHEN OTHERS.
  39.     ENDCASE.
  40.   ENDDO.
  41.   GET RUN TIME FIELD t2.
  42.   DO 10000 TIMES.
  43.     IF sy-index EQ  1.
  44.     ELSEIF sy-index EQ  2.
  45.     ELSEIF sy-index EQ  3.
  46.     ELSEIF sy-index EQ  4.
  47.     ELSEIF sy-index EQ  5.
  48.     ELSEIF sy-index EQ  6.
  49.     ELSEIF sy-index EQ  7.
  50.     ELSEIF sy-index EQ  8.
  51.     ELSEIF sy-index EQ  9.
  52.     ELSEIF sy-index EQ  11.
  53.     ELSEIF sy-index EQ  12.
  54.     ELSEIF sy-index EQ  13.
  55.     ELSEIF sy-index EQ  14.
  56.     ELSEIF sy-index EQ  15.
  57.     ELSEIF sy-index EQ  16.
  58.     ELSEIF sy-index EQ  17.
  59.     ELSEIF sy-index EQ  18.
  60.     ELSEIF sy-index EQ  19.
  61.     ELSEIF sy-index EQ  20.
  62.     ELSE.
  63.     ENDIF.
  64.   ENDDO.
  65.   GET RUN TIME FIELD t3.
  66.   ls_result-case = t2 - t1.
  67.   ls_result-ifelse = t3 - t2.
  68.   IF ls_result-case < ls_result-ifelse.
  69.     ls_result-percent = 100 * ( ls_result-ifelse - ls_result-case ) / ls_result-case.
  70.     ls_result-faster = 'Case'.
  71.   ELSE.
  72.     ls_result-percent = 100 * ( ls_result-case - ls_result-ifelse ) / ls_result-case.
  73.     ls_result-faster  = 'IfElse'.
  74.   ENDIF.
  75.   APPEND ls_result TO lt_result.
  76.   CLEAR ls_result.
  77. ENDDO.
  78. *
  79. DATA lr_alv TYPE REF TO cl_salv_table.
  80. TRY .
  81.     cl_salv_table=>factory(
  82.       IMPORTING
  83.         r_salv_table   = lr_alv
  84.       CHANGING
  85.         t_table        = lt_result
  86.     ).
  87.   CATCH cx_salv_msg       ##no_handler.
  88. ENDTRY.
  89. *activate toolbar buttons
  90. DATA: lr_functions TYPE REF TO cl_salv_functions_list.
  91. lr_functions = lr_alv->get_functions( ).
  92. lr_functions->set_default( abap_true ).
  93. *optimize the column width
  94. DATA: lr_columns TYPE REF TO cl_salv_columns.
  95. lr_columns = lr_alv->get_columns( ).
  96. lr_columns->set_optimize( abap_true ).
  97. *display table as ALV
  98. lr_alv->display( ).

/.

0 Kudos

Good one

But how much is the difference in runtime if you reduce the number of WHEN- & the ELSEIF- blocks to (a more practical) value of 5 for example 🙂

0 Kudos

Conditions 13 and 17 seem to run a little slower than the others, that's probably because the kernel performs a localisation check, moving to 14 and 18 respectively in countries where the former numbers are considered unlucky.

Otherwise a great-looking piece of code!

Thomas

0 Kudos

IF 5 blocks are used, difference may be littler in that CASE.

0 Kudos

Roger that, Captain!

Former Member
0 Kudos

Hi Manisha,

I just modified the colored code, just try this in your system and see any performance

1. Declare one more field flag type c in  lt_sn_eh

2. Declare internal table lt_sn_eh as i mentioned


data lt_sn_eh type TABLE OF xxxxx with NON-UNIQUE SORTED KEY  sn_counter COMPONENTS zsng2g_sn_counter.

*--Filling tbl_range


ls_sn_eh-flag = abap_true. " to make required entries

LOOP AT gt_pkt_eh INTO ls_pkt_eh.                   "where ZSNG2G_PACKET_TYPE = 'RANGE'.

   IF ls_pkt_eh-zsng2g_packet_type = 'RANGE'.

     ls_range-range_from = ls_pkt_eh-zsng2g_range_start.

     ls_range-range_to     = ls_pkt_eh-zsng2g_range_end.

     APPEND ls_range TO lt_range.

   ELSEIF ls_pkt_eh-zsng2g_packet_type = 'LIST' .

*--Fill tbl_list

     MODIFY lt_sn_eh FROM ls_sn_eh USING KEY sn_counter TRANSPORTING flag  WHERE zsng2g_sn_counter GE ls_pkt_eh-zsng2g_range_start AND

   zsng2g_sn_counter LE ls_pkt_eh-zsng2g_range_end.

   ENDIF.

ENDLOOP.

LOOP AT lt_sn_eh ASSIGNING <fs_sn_eh> USING KEY sn_counter.

  if <fs_sn_eh>-flag = abap_true.

   ls_list-table_list = ls_sn_eh-zsng2g_sn_serial_number.

   APPEND ls_list TO lt_list.

   CLEAR ls_list.

endif.

ENDLOOP.