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

Performence issue ( Nested loop )

Former Member
0 Likes
786

Hi all ,

I have 2 Itables with similar structure( f1,f2,f3,f4 ).

i want to compare the field (f4) of two itables if (f1 ,f2, f3 ) is equal. if (f4) is not same then i should move this to itable c.

Example : itab1 itab2

A B C X A B C Y

c c d X A B C Z

c c d X

Finally i have to display it as A B C X

A B C Y

A B C X

A B C Z

__________

I did like this.

loop at itab1 .

loop at itab2 into w_itab

where f1 = itab1-f1

f2 = itab1-f2

f3 = itab1-f3.

append w_itab to itab3 .

endloop. (itab2)

if sy-subrc eq 0 .

loop at itab3 .

if itab1-f4 NE itab3-f4 .

Error-f1 = itab1-f1 .

Error-p1 = itab3-f1 .

.

.

append Error.

endif.

endloop.(itab3)

endif.

endloop. (itab1)

********

i got the performence issue here . can anybody suggest alternate solution for this.

Thanks.

1 ACCEPTED SOLUTION
Read only

FredericGirod
Active Contributor
0 Likes
764

With this method you will make only one loop in the second table. And you will read only one time the table 2.

It's very faster when you have a lot of entries.

loop at itab_1.
  loop at itab_2 from w_tabix. 
    if itab_1-f1 eq itab_2-f1.
      process.
    elseif itab_1-f1 lt itab_2-f1.
      exit.
    endif. 
    move sy-tabix to w_tabix.
  endloop.
endloop.

Fred

Message was edited by: Frédéric Girod

7 REPLIES 7
Read only

FredericGirod
Active Contributor
0 Likes
765

With this method you will make only one loop in the second table. And you will read only one time the table 2.

It's very faster when you have a lot of entries.

loop at itab_1.
  loop at itab_2 from w_tabix. 
    if itab_1-f1 eq itab_2-f1.
      process.
    elseif itab_1-f1 lt itab_2-f1.
      exit.
    endif. 
    move sy-tabix to w_tabix.
  endloop.
endloop.

Fred

Message was edited by: Frédéric Girod

Read only

Former Member
0 Likes
764

Loop at itab1.

lf4 = itab1-f4.

at end of f3.

loop at itab2 where f1 = itab1-f1 and

f2 = itab1-f2 and f3 = itab-f3 and

f4 <> lf4.

move information to Error.

append error.

endloop.

endat.

endloop.

Read only

former_member186741
Active Contributor
0 Likes
764

types: begin of ti,

f1(1),

f2(1)

f3(1),

f4(1),

end of ti.

data: itab1 type sorted table of ti with header line

with non-unique key f1 f2 f3 f4.

data: itab2 type sorted table of ti with header line

with non-unique key f1 f2 f3 f4.

data: itab3 type sorted table of ti with header line

with non-unique key f1 f2 f3 f4.

loop at itab1.

loop at itab2 where f1 = itab1-f1

and f2 = itab1-f2

and f3 = itab1-f3

and f4 <> itab1-f4.

append itab2 to itab3.

endloop.

endloop.

loop at itab3.

read table itab1 with key f1 = itab3-f1

f2 = itab3-f2

f3 = itab3-f3.

write:/ itab1-f1,itab1-f2,itab1-f3,itab1-f4.

write:/ itab3-f1,itab3-f2,itab3-f3,itab3-f4.

skip.

endloop.

Read only

Former Member
0 Likes
764

One of the solutions which will help you improve performance irrespective of size of internal tables:

sort itab2 by f1 f2 f3.

loop at itab1 into wa_itab1.

read table itab2 with key f1 = wa_itab1-f1

f2 = wa_itab1-f2

f3 = wa_itab1-f3

transporting no fields.

if sy-subrc = 0.

loop at itab2 into wa_itab2 from sy-tabix.

if wa_itab2-f1 <> wa_itab1-f1

or wa_itab2-f2 <> wa_itab2-f2

or wa_itab2-f3 <> wa_itab3-f3.

if wa_itab2-f4 <> wa_itab1-f4.

append wa_itab2 to itab3.

endif.

endif.

endloop.

endif.

In case itab2 is much more larger then expected, loop at... where... will give you poor performance.

Let me know if you have any further queries.

Don't forget to mark helpful answers!

Gaurav Parmar.

endloop.

Read only

Former Member
0 Likes
764

This is a duplicate post. Please check my response to your other post and close one or both of them.

Rob

Read only

Former Member
0 Likes
764

Hi Prithvi,

Try with teh below logic, definetely it will improve the performance.

sort itab1 by a b c.

sort itab2 by a b c.

loop at itab1.

read table itab2 with key a = itab1-a

b = itab1-b

c = itab1-c

binary search.

loop at itab2 index sy-tabix.

if itab2-a <> itab1-a or

itab2-b <> itab1-b or

itab2-c <> itab1-c.

exit.

endif.

your code...

endloop.

endloop.

Hope this will help you.

Regards,

Satya.

Read only

Former Member
0 Likes
764

Hi,

try like this.......

sort tab1.

sort tab2.

loop at tab1.

read table tab2 with key f1 = tab1-f1

f2 = tab1-f2

f3 = tab1-f3

binary search.

if tab2-f1 <> tab2-f1 or

tab2-f2 <> tab2-f2 or

tab2-f3 <> tab2-f3 .

Continue.

else.

append to fields to tab3.

endif.

endloop.

Madhavi