2008 May 05 11:57 AM
Hi All,
I have a question here (kinda stupid)...I wrote two codes..both return me same result.
But I have to know which way better....The Loop V.S The Read.
Below are my codes:
The Loop way........
LOOP AT it_table WHERE mtart = 'ZTG' OR mtart = 'ZPM'.
EXIT.
ENDLOOP.
IF sy-subrc = 0.
READ TABLE it_table WITH KEY mtart = 'ZSM' BINARY SEARCH.
IF sy-subrc = 0.
lv_flag = 1.
ELSE.
lv_flag = 0.
ENDIF.
ELSE.
lv_flag = 0.
ENDIF.and the Read way.....
READ TABLE it_table WITH KEY mtart = 'ZTG' BINARY SEARCH.
IF sy-subrc NE 0.
READ TABLE it_table WITH KEY mtart = 'ZPM' BINARY SEARCH.
IF sy-subrc = 0.
lv_flag1 = 0.
ELSE.
lv_flag1 = 1.
ENDIF.
ELSE.
lv_flag1 = 0.
ENDIF.
READ TABLE it_table WITH KEY mtart = 'ZSM' BINARY SEARCH.
IF sy-subrc = 0.
IF lv_flag1 = 0.
lv_flag = 1.
ELSE.
lv_flag = 0.
ENDIF.
ELSE.
lv_flag = 0.
ENDIF.Can someone please tell me which way better?
The fastest and most effective.
I tried SE30, and I got different duration every time I run above codes.
I tried ST05, I can't get what I want....the duration of these codes...
Please advice and help.
Thanks.
Cheers,
Law
2008 May 05 12:03 PM
Hi,
Read statement with binary search is the best option...
But make sure that ur funtionality is fine....
If both the codes work in the same way , go for the second one...
regards,
lavanya
Hmm...but the 2nd way uses more indicator (lv_flag1) and more lines of codes. Doesn't this affected the performance as well?
By the way....before perform both ways, the internal table was sorted by the field mtart.
2008 May 05 12:03 PM
Hi,
Read statement with binary search is the best option...
But make sure that ur funtionality is fine....
If both the codes work in the same way , go for the second one...
regards,
lavanya
2008 May 05 12:06 PM
Hmm...but the 2nd way uses more indicator (lv_flag1) and more lines of codes. Doesn't this affected the performance as well?
By the way....before perform both ways, the internal table was sorted by the field mtart.
2008 May 05 12:12 PM
Go for the second way...even SAP recommends read table compared to loop at..
2008 May 05 12:12 PM
hi check this..
the main difference between the loop and read..
suppose i had one record for every record there may be so many records ...like a record in the mara-matnr there may be many in the mard table ..in this case we use the loop in the loop..
loop at it_mara .
loop at it_mard where matnr = it_mara-matnr.
it_final[] = it_mard[].
append it_final.
endloop.
endloop.
for every record if there is only one record...the we will use the read statement...in the loop..
loop at it_mara .
read table it_marc with key matnr = it_mara-matnr.
endloop.
regards,
venkat.
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |