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

Looking for Optimized code...

Former Member
0 Likes
816

Hi,

I have 2 internal tables, say, A & B. The records in B is subset of records in A. Now I want to find out the set of records which are in A, but not in B. I'm planning to do like below(pseudocode). Any other optimized way possible?

LOOP at A.

READ table B (using binary search).

IF sy-subrc = 0.

...

ELSE.

...

ENDIF.

ENDLOOP.

Thanks & Regards,

Sree

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
669

Hi,

If i assume that ur internal tables have a key field to identify the unique record for example material number.

then...

Loop at itab2.

populate the key field value into a range table r_matnr

with other fields like opt = 'EQ' and sign = 'I'.

endloop.

Now u can use.

delete itab1 where not matnr in r_matnr.

this is advisable if the entries are very high,

Or ur code can be optimised as...

Since ur saying that B is subset of A. (assuming that number of entries in B comparatively less than in A)

u can do..

Loop at B.

Read table A (using binary search).

if sy-subrc eq 0.

delte table A index sy-tabix.

endif.

endloop.

6 REPLIES 6
Read only

Former Member
0 Likes
670

Hi,

If i assume that ur internal tables have a key field to identify the unique record for example material number.

then...

Loop at itab2.

populate the key field value into a range table r_matnr

with other fields like opt = 'EQ' and sign = 'I'.

endloop.

Now u can use.

delete itab1 where not matnr in r_matnr.

this is advisable if the entries are very high,

Or ur code can be optimised as...

Since ur saying that B is subset of A. (assuming that number of entries in B comparatively less than in A)

u can do..

Loop at B.

Read table A (using binary search).

if sy-subrc eq 0.

delte table A index sy-tabix.

endif.

endloop.

Read only

0 Likes
669

Hi Sharath,

Using a ranges table is an impressive solution. And I think it is the best approach, primarily due to the fact that there's only one LOOP (to create a range).

Sree: This is a good solution. You must try implementing it in case your internal table has a key comrpising a single field.

Regards,

Anand Mandalika.

Read only

0 Likes
669

Thanks you all! very interesting solutions I got which I was looking for !

In my case, I have to go with Patrik's suggestions as my table has 2 non-unique keys.

I will keep the use of Ranges in my mind for some other place !

Thanks & Regards,

Sree

Read only

0 Likes
669

On doing little more analysis, I found that there will be only one key for the Internal table and hence usign Sharath's solution!

Thanks all again!

Regards,

Sree

Read only

andreas_mann3
Active Contributor
0 Likes
669

Hi Sree,

looks like , that there's nothing to optimize.

regards Andreas

Read only

Former Member
0 Likes
669

Sree,

I stumbled around in the ABAP documentation trying to find a suitable command for a 'difference set' operation - because that is essentially what you are trying to achieve - but I did not succeed.

Nevertheless when analyzing the computational complexity of your program one can say that it is very efficient.

Binary search is really fast (log(n) precisely) but can only be applied to comparable, ordered and properly sorted data. Assuming this is the case with B then your algorithm has a total complexity of n*log(n) which is good.

There are nevertheless two optimizations I would like to present to you:

1. Do the binary search in the larger of the two tables and consequentially loop over the smaller one.

2. Concerning the 'target' of your operation: The entries that you search for (i.e. the ones that do not exist in both tables) will probably be written into a table C. It makes sense to create this table with an explicit initial size which is max(size(A),size(B))-min(size(A),size(B)) (pseudocoded, size(A) and size(B) of course in local variables ) to prevent continuous memory allocation which is very time consuming in comparison to the search algorithm

Regards,

Patrick