Quantcast
Channel: ABAP Development
Viewing all articles
Browse latest Browse all 948

Simple algorithm - Nested looping

$
0
0

Hi all,

 

Here im going to show you the difference of straightforward nested loop and most parallel cursor method of looping

 

First let us take a example of straight forward nested loop.

 

EX:

* Entries: 100 (ITAB1), 1000 (ITAB2)

* Line width: 100

* Both tables sorted by key K

 

 

LOOP AT ITAB1 INTO WA1.

  LOOP AT ITAB2 INTO WA2

                WHERE K = WA1-K.

    " ...

  ENDLOOP.

ENDLOOP.

 

 

Ex:of Parallel cursor loop

 

I = 1.

LOOP AT ITAB1 INTO WA1.

  LOOP AT ITAB2 INTO WA2 FROM I.

    IF WA2-K <> WA1-K.

      I = SY-TABIX.

      EXIT.

    ENDIF.

    " ...

  ENDLOOP.

ENDLOOP.

 

 

 

If ITAB1 has n1 entries and ITAB2 has n2 entries, the time needed for  the nested loop with the straightforward algorithm is O(n1 * n2),  whereas the parallel cursor approach takes only O(n1 + n2) time. The above parallel cursor algorithm assumes that ITAB2 contains only entries also contained in ITAB1.

If this assumption does not hold, the parallel cursor algorithm gets slightly more complicated, but its performance characteristics remain the same.

 

And when you check the measure runtime of nested loop is 10.538 mirco seconds where as by using the parallel cursor loop runtime is only 272  micro seconds


Viewing all articles
Browse latest Browse all 948

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>