Application Development and Automation 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: 
Read only

Which is better code ? Performance wise

Former Member
0 Likes
801

Hi,

Which one of the following is the better code by considering performance ?

The diffrence between them is first is with LOOP and READ.. WHILE ... ENDWHILE.

2nd one is with LOOP and collect, again LOOP inside another LOOP.

Please let me know if you have any questions.

-


CODE I

-


LOOP AT it_matnr INTO wa_matnr.

MOVE: wa_matnr-matnr TO wa_final-matnr,

wa_matnr-maktx TO wa_fianl-maktx.

READ TABLE it_middle WITH KEY matnr = wa_matnr

BINARY SEARCH.

l_subrc = sy-subrc.

l_index = sy-tabix.

WHILE l_subrc EQ 0.

CASE it_middle-pstyv.

WHEN c_ziss.

wa_final-qty1 = it_middle-lmeng.

WHEN c_zret.

wa_final-qty2 = it_middle-lmeng.

WHEN c_zcto.

wa_final-qty3 = it_middle-lmeng.

WHEN c_zcti.

wa_final-qty4 = it_middle-lmeng.

WHEN c_zson.

wa_final-qty5 = it_middle-lmeng.

WHEN c_zsof.

wa_final-qty6 = it_middle-lmeng.

WHEN c_zion or c_ziof.

wa_final-qty7 = it_middle-lmeng.

ENDCASE.

COLLECT wa_fianl INTO it_final.

CLEAR: wa_final-qty1, wa_final-qty2, wa_final-qty3,

wa_final-qty4, wa_final-qty5, wa_final-qty6,

wa_final-qty7.

l_matnr = it_middle-matnr.

l_index = l_index + 1.

READ TABLE it_middle INDEX l_index.

IF l_matnr <> it_middle-matnr OR sy-subrc NE 0.

l_subrc = 9.

exit.

ENDIF.

ENDWHILE.

MOVE: wa_matnr-op_bal TO wa_final-op_bal,

wa_matnr-cl_bal TO wa_final-cl_bal.

COLLECT wa_final INTO it_fianl.

ENDLOOP.

-


CODE II

-


SORT it_middle BY matnr pstyv.

LOOP AT it_middle INTO wa_middle.

COLLECT wa_middle INTO it_middle1.

ENDLOOP.

LOOP AT it_matnr INTO wa_matnr.

MOVE: wa_matnr-matnr TO wa_final-matnr,

wa_matnr-maktx TO wa_fianl-maktx,

wa_matnr-op_bal TO wa_final-op_bal,

wa_matnr-cl_bal TO wa_final-cl_bal.

LOOP AT it_middle WHERE matnr = wa_matnr-matnr.

CASE it_middle-pstyv.

WHEN c_ziss.

wa_final-qty1 = it_middle-lmeng.

WHEN c_zret.

wa_final-qty2 = it_middle-lmeng.

WHEN c_zcto.

wa_final-qty3 = it_middle-lmeng.

WHEN c_zcti.

wa_final-qty4 = it_middle-lmeng.

WHEN c_zson.

wa_final-qty5 = it_middle-lmeng.

WHEN c_zsof.

wa_final-qty6 = it_middle-lmeng.

WHEN c_zion or c_ziof.

wa_final-qty7 = it_middle-lmeng.

ENDCASE.

COLLECT wa_fianl INTO it_final.

ENDLOOP.

IF sy-subrc NE 0.

COLLECT wa_final INTO it_fianl.

ENDIF.

ENDLOOP.

Thank you

Surya

6 REPLIES 6
Read only

Former Member
0 Likes
714

Hi Surya,

If both the internal tables will be going to have a lots of data then first one is efficient. And if it is a small amount of data then use second one as it is more readable code and there will be no performance issue for small data.

Regards,

Atish

Read only

Former Member
0 Likes
714

Hi,

Use parallel cursor technique in your code II.

Read only

0 Likes
714

What is Parallel coursor technique ?

Thank you,

Surya

Read only

Former Member
0 Likes
714

Surya,

If both the internal tables will be going to have a lots of data then first one is efficient. And if it is a small amount of data then use second one as it is more readable code and there will be no performance issue for small data.

Regards,

Read only

Former Member
0 Likes
714

Surya,

Performance wise, the First one will be a better choice.

Because, loop inside loop has to be avoided since it will drain the performance.

Thanks,

Madhan.

Read only

rodrigo_paisante3
Active Contributor
0 Likes
714

Hi,

you will see good loop performance in these blogs:

/people/rob.burbank/blog/2006/02/07/performance-of-nested-loops

/people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables

If you want to see program performance, try this

/people/siegfried.boes/blog/2007/11/13/the-abap-runtime-trace-se30--quick-and-easy

All the best,

Rodrigo Paisante