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

reading internal table with binary search

Former Member
0 Likes
1,207

Hi friends,

can u differentaite between the below 2 statements and suggest me which one to use.

code :

start-of-selection.

select matnr

ersda

ernam

from mara into

table t_mara where

matnr in s_matnr.

if t_mara[] is not initial.

select matnr

werks

from marc into

table t_marc

for all entries in t_mara

where matnr = t_mara-matnr.

endif.

sort t_marc by matnr.

**********************************************************************

a)

loop at t_mara into wa_mara.

read table t_marc into wa_marc with key matnr = wa_mara-matnr binary search.

endloop.

**********************************************************************

b)

loop at t_mara into wa_mara.

read table t_marc into wa_marc with key matnr = wa_mara-matnr .

endloop.

**********************************************************************

can any one tell what exactly will happen if i use binary search and if i dnt use binary search.

which is preferable .

Regards,

PRIYANKA.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,129

Hi Priyanka,

Binary Search is faster than Sequential Search.

when you write


read table t_marc into wa_marc with key matnr = wa_mara-matnr .

It search the Internal table in a sequential manner, i.e, first record, second record ....last record, till it find the right value.

But, if you write the code as


read table t_marc into wa_marc with key matnr = wa_mara-matnr binary search.

It will search the internal table in BINARY search algorith.

Table has to sorted in ascending format before we do binary search.

In sequential search, the graph between number of records in the internal table Vs time taken is linear.

In Binary search, the graph between number of records in the internal table Vs time taken is logarithmic.

9 REPLIES 9
Read only

ThomasZloch
Active Contributor
0 Likes
1,129

If t_marc is defined as a SORTED table with key MATNR, you can omit BINARY SEARCH and just read the table with TABLE KEY matnr.

If t_marc is a STANDARD table, you better sort it by MATNR and use BINARY SEARCH when accessing it.

Thomas

Read only

Former Member
0 Likes
1,129

Hi Priyanka,

Its a good idea to SORT the internal table first and then read using Binary search.

Sort the table on the key that will be used in the READ.

This is helpful performance wise.

reagrds,

Pranu

Read only

Former Member
0 Likes
1,129

Hi Priyanka,

loop at t_mara into wa_mara.

read table t_marc into wa_marc with key matnr = wa_mara-matnr binary search.

endloop.

The above method is preferrable because Binary Search increases the performance.

Basically in the Binary Search process, the whole records are divided into two halves and based on the condition, ie., key it searchs only the correct half. So, our search time has been cut down to half of the time. Now the same process continues until the correct record is found. Every time it divides into two halves and searches only the correct half. So, our search time gradually reduces.

But we need to make sure that the table should be SORTED on which the Binary Search is being performed. Otherwise, we may get wrong results.

Hope this will help.

Regards,

Nitin.

Read only

Former Member
0 Likes
1,129

Hello

Binary search is accelerates data selection from internal table.

The statement a) will be work faster.

Read only

Former Member
0 Likes
1,129

Hi

whenever you use BINARY SEARCH in read statement the search will be more faster and that improves the performance of the program a lot.

here the only condition is the internal table must be SORTED on the fields which are used in READ.

In your case..

sort t_marc by matnr.

**********************************************************************

a)

loop at t_mara into wa_mara.

read table t_marc into wa_marc with key matnr = wa_mara-matnr binary search.

endloop.

the above code will reduce the no. of searches for a Material to root value of the no. of entires in the internal table like 10 searches for 100 entry table where as your option B code will take 100 searches.

so READ with BINARY SEARCH is always recommended.

Read only

0 Likes
1,129

we can use binary search one. i hope it is not an obsolute statement.

Iam i correct.....?

Regards,

PRIYANKA.

Read only

0 Likes
1,129

BINARY SEARCH is not obsolete. You can definitely use this.

This will improve the performance of the code.

Read only

0 Likes
1,129

Hi,

Yes, you are correct. Make use of A.

Thanks,

Sriram Ponna.

Read only

Former Member
0 Likes
1,130

Hi Priyanka,

Binary Search is faster than Sequential Search.

when you write


read table t_marc into wa_marc with key matnr = wa_mara-matnr .

It search the Internal table in a sequential manner, i.e, first record, second record ....last record, till it find the right value.

But, if you write the code as


read table t_marc into wa_marc with key matnr = wa_mara-matnr binary search.

It will search the internal table in BINARY search algorith.

Table has to sorted in ascending format before we do binary search.

In sequential search, the graph between number of records in the internal table Vs time taken is linear.

In Binary search, the graph between number of records in the internal table Vs time taken is logarithmic.