‎2008 Sep 05 8:42 AM
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.
‎2008 Sep 05 9:00 AM
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.
‎2008 Sep 05 8:44 AM
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
‎2008 Sep 05 8:49 AM
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
‎2008 Sep 05 8:52 AM
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.
‎2008 Sep 05 8:54 AM
Hello
Binary search is accelerates data selection from internal table.
The statement a) will be work faster.
‎2008 Sep 05 8:56 AM
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.
‎2008 Sep 05 9:03 AM
we can use binary search one. i hope it is not an obsolute statement.
Iam i correct.....?
Regards,
PRIYANKA.
‎2008 Sep 05 9:07 AM
BINARY SEARCH is not obsolete. You can definitely use this.
This will improve the performance of the code.
‎2008 Sep 05 9:12 AM
‎2008 Sep 05 9:00 AM
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.