1

I'm interviewing for a job, and on the test they sent me they provided the following image of 2 ABAP loops on an internal table, and asked which loop will execute faster: enter image description here

After checking heavily on the internet I came to this 2 different outcomes:

  • on the one hand, as for what I found online, the "WHERE" statement is faster than the logical if statement, due to the fact that it directly processes the records satisfying the condition.
  • on the other hand, it seems as the 2 loops are doing different things and that the right loop is basically jumping ahead whenever WA2-K != WA1-K, so I believe execution is very dependent on the provided tables.

Am I missing something? is there an obvious 1 answer to this question?

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • Of course the first will be faster. The K column is a key of a sorted table. In the second one you have a peculiar condition which has to be evaluated every time, this is definitely slower than accessing the table directly with its key. I would be very surprised if the answer was different. – Jagger Jun 26 '21 at 12:36
  • 1
    It turned out I was wrong. :) – Jagger Jun 26 '21 at 15:41
  • The short answer would be: The WHERE condition always has to scan the whole ITAB2 to find the relevant keys, the parallel cursor approach only starts from the last found key and only has to check the upcomming matching keys because of sorting. But parallel cursor is not a always-working-general-approach and is very dependent on the data and the goal – futu Jun 27 '21 at 15:58
  • 4
    To add to @futu's comment, not something they were asking, but the code on the left is more robust. It will always process all the relevant entries in the second table. The code on the right will not do that if there is a duplicate key in table 1 or if one of the keys in table 1 does not exist in table 2. This is not something inherently wrong with parallel cursors, just something with the crude implementation presented here. – Gert Beukema Jun 27 '21 at 19:29
  • 2
    Oh, and when answering questions for an ABAP job do not use !=, it's <> or NE. Good luck! – Gert Beukema Jun 27 '21 at 19:31
  • @GertBeukema your explanation is exactly what I meant :> – futu Jun 29 '21 at 19:51

1 Answers1

3

Your screenshot comes from the program RSHOWTIM (or SE38 > menu Environment > Examples -> Performance examples), which is available in all ABAP-based software.

The two algorithms are doing exactly the same thing (*), but one is more performing than the other. SY-TABIX is the line number of the last iterated line inside the LOOP AT block (providing that it's a standard or sorted table).

(*) see Gert comment

The full screen is this one (in ABAP 7.52): SAP RSHOWTIM Nested loops

NB: you could install an ABAP trial system on your computer if you need to train. Or you can use Cloud trials (you have then to pay for the network/disk usage).

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • 3
    The two programs will only do the exact same thing if there are no duplicate entries for K in ITAB1 and there are no entries for K in ITAB2 that are not in ITAB1. And thank you for the way more readable screenshot :-) – Gert Beukema Jun 27 '21 at 19:37
  • 2
    Small note: The left example of the release you are on is a bad example, as the nested loop could perform in O(n1 * log2( n2 ) ) if the WHERE condition [would use binary search](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenitab_where_optimization.htm). The example has been changed for newer releases. – Jonas Wilms Jun 29 '21 at 12:48