Skip to Content
author's profile photo Former Member
Former Member

LOOP within a SUBROUTINE/METHOD/FM within a LOOP.

Just a random stupid question. LOOP within a LOOP is somewhat a NO-NO and must be avoided 'whenever' possible. So does the title above applies to the rule ?!

Visual:

LOOP.

.....

PERFORM subroutine.

.....

ENDLOOP.

FORM subroutine.

.......

LOOP.

........

ENDLOOP.

........

ENDFORM.

Add a comment
10|10000 characters needed characters exceeded

Related questions

7 Answers

  • Best Answer
    Posted on May 03, 2011 at 06:19 AM

    Hi Jay,

    in your coding the inner loop is still inside the outer one, no matter how much modularization units you

    put in between.

    As Suhas said the goal is not to avoid loops in loops but to avoid quadratic (or worse) run time behaviour.

    In generall we do not want to process all rows of an inner loop for each line of the outer one ( n * n = On²).

    The inner loop shall be an optimized one with e.g. a logarithmic ( = O log(n)) or constant ( = O (1)) run time

    behaviour so that we don't end up with n * n but with something better: n * log n or n * 1.

    The general rule "nested loops are bad" comes from the fact that with nested loops there is a high probaility

    of quadratic run time behaviour if the inner loops are not optimized ones. Nested loops can sometimes (often) not

    be avoided, but they can be optimized (usually) 😉

    Kind regards,

    Hermann

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 03, 2011 at 05:59 AM

    Hello Jay,

    The code you've posted is a dirty trick to hide your nested LOOPs.

    LOOP within a LOOP is somewhat a NO-NO and must be avoided 'whenever' possible

    I have encountered 100's of scenarios where nested LOOPs can't be avoided. Nested LOOPs can be optimised by:

    1. Using SORTED TABLEs in conjunction with LOOP...WHERE. This is less error-prone & my favorite.

    2. Using the "outdated" parallel cursor method. Yyou can be in a mess if the inner LOOP is not EXITed using proper conditions.

    Hope this helps.

    BR,

    Suhas

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 04, 2011 at 02:05 PM

    and be aware, that many LOOP in LOOP are completely o.k., as one of the LOOPs checks only a number of options, cases or whatever, so the scaling is N*c and c fixed.

    The SORTED table is no trick, it is the most recommended out of the options, optimize a basis of BINARY SEARCH, use hashed table (if key is unique) or parallel cursor (complcated).

    If you task is a

    LOOP and corresponding LOOP at WHERE then also think about whether you can ge rid of the form, it is an overhead if called extremely often.

    Siegfried

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 06, 2011 at 01:32 PM

    Hi Jay,

    Instead of using nested loop, you can use the following logic by applying parallel cursor.

    It will also improve the performance.

    sort itab2 ASCENDING BY field1.
    
    LOOP AT itab1 INTO wa_itab1.
    READ TABLE itab2 WITH KEYfield1 = wa_itab1-field BINARY SEARCH TRANSPORTING NO FIELDS.
     IF sy-subrc = 0.
       LOOP AT itab2 INTO wa_itab2 FROM sy-tabix.
        IF wa_itab2-field1 NE wa_fitab1-field.
           EXIT.  "    --->  This logic will minimize the loop passes
        ENDIF.
       ENDLOOP.
     ENDIF.
    ENDLOOP.

    Thanks,

    Suchandra

    Edited by: Matt on May 9, 2011 9:08 AM - use tags - it improves readability

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 07, 2011 at 04:43 AM

    Hi,

    Better apply parallel cursor. It will also improve the performance. Ref the below link

    http://wiki.sdn.sap.com/wiki/display/Snippets/ABAPCodeforParallelCursor-Loop+Processing

    hope it will useful.

    Regards,

    Dhina..

    Add a comment
    10|10000 characters needed characters exceeded

    • @Yuri,

      Does the key in the sorted table has to be unique?
      

      You can define unique or non-unique key.

      @Dhina DMD,

        
      Better apply parallel cursor. It will also improve the performance.
      

      Yes, It will improve the performance but it handling loop is over head and complicated exturnal procedure.

      Better to used sorted table where it very ease write code simply without any complication work line parallel cursor method internally.

      I will recommand to don't use the parallel cursor method and use the sorted table instead of it.

      Kind Rgds

      Ravi Lanjewar

  • Posted on Jun 09, 2011 at 08:03 AM

    Better apply parallel cursor. It will also improve the performance. Ref the below link

    http://wiki.sdn.sap.com/wiki/display/Snippets/ABAPCodeforParallelCursor-Loop+Processing

    This is not the parallel cursor method!!! The parallel cursor is different, works with 2 loops and a complicated logic on the indexes.

    The wiki explains the solution with the sorted standard table, using BINARY SEARCH, LOOP FROM INDEX, EXIT. This is o.k., but cumbersome and also outdated. The sorted table does the same automatically!

    The key will be non-unique in most cases, if you define a unique key you can run into trouble if duplicate keys appear.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 19, 2011 at 07:02 AM

    tenx

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.