10-17-2008 7:42 AM
Hi All,
I have a generic query.Which is better performance wise,
use of CASE ... ENDCASE or use of IF ELSEIF ENDIF.
If i have multiple else is it better to use Case statement.
Please suggest.
Thanks in Advance,
Saket.
10-17-2008 7:45 AM
CASI is slightly better performance wise. However there are cases when you cannot substitute IF..ELSEIF..ENDIF with CASE, because of the conditions (for example when you have to compare a value with a range)
10-17-2008 8:13 AM
Hi Eric,
is there really a measurable effect compared to the overall runtime of a typical program (some DB access, some internal table handling)?
I think this decision should be made purely in respect to functionality and readability of the program code.
Cheers
Thomas
10-17-2008 8:40 AM
hi Thomas,
I don't know if it can be measured at all, definetly it is only microseconds (or even less). If there are performance problems in a program, it is not because of unproper use of IF....
However it is stated in SE38 ==> Environment ==> Examples ==> Performance examples : "CASE statements are clearer and a little faster than IF-constructions. "
Of course I fully agree on your statement about "functionality and readability", I personally prefer CASE, whenever possible
10-17-2008 9:20 AM
Hi,
CASE..ENDCASE will be having Higher Performance. You can use that.
Regards,
Neenu
10-17-2008 10:09 AM
10-17-2008 12:30 PM
Hi,
if you are checking multiple values in single field then use CASE statment.
if you are checking multiple values in multiple fields then use IF ELSEIF statment.
10-17-2008 12:47 PM
Hi,
In case you are putting more than 5 IF statements within a Processing Block...it is advisable to use CASE Statement....
Case is easy to understand and better in performance....use Exit statement in the end of every WHEN code block....so as to save prog from unnecessary processing......
10-17-2008 12:54 PM
>
use Exit statement in the end of every WHEN code block....so as to save prog from unnecessary processing......
???
My ABAP jumps to the next statement after ENDCASE whenever a WHEN was true (and the code part in the WHEN finished).
10-17-2008 1:06 PM
Hi,
I believe you are talking about a CASE Statement inside a Loop..Endloop.......whereas I am talking about a different situation....
Like
At-Selection Screen.
Case Sy-lsind.
When '1'.
10-17-2008 1:16 PM
OK, I see what you mean, but in your example the EXIT will cause that the program continues with the next event (will jump out from AT SELECTION-SCREEN completely), which is most of the time is not what we want...
10-17-2008 1:18 PM
Hi Eric,
I got it.....
I will try to be more specific next time.....
Thanks for this
10-17-2008 4:22 PM
Hi,
performance wise its better to go with CASE....ENDCASE than with IF ELSEIF ENDIF.
Thanks,
abaper007
10-17-2008 4:43 PM
Hmmmm...
Rather than giving opinions, let's find out:
REPORT ztest LINE-SIZE 80 MESSAGE-ID 00.
DATA: f(1) VALUE 'E',
start TYPE i,
end TYPE i,
dif TYPE i.
DO 5 TIMES.
GET RUN TIME FIELD start.
IF f = 'A'.
ELSEIF f = 'B'.
ELSEIF f = 'C'.
ELSEIF f = 'D'.
ELSEIF f = 'E'.
ELSEIF f = 'F'.
ELSEIF f = 'G'.
ELSEIF f = 'H'.
ELSEIF f = 'I'.
ELSEIF f = 'J'.
ENDIF.
GET RUN TIME FIELD end.
dif = end - start.
WRITE: /001 'Time for IF/ELSEIF', ':', dif, 'microseconds'.
GET RUN TIME FIELD start.
CASE f.
WHEN 'A'.
WHEN 'B'.
WHEN 'C'.
WHEN 'D'.
WHEN 'E'.
WHEN 'F'.
WHEN 'G'.
WHEN 'H'.
WHEN 'I'.
WHEN 'J'.
ENDCASE.
GET RUN TIME FIELD end.
dif = end - start.
WRITE: /001 'Time for CASE ', ':', dif, 'microseconds'.
SKIP 1.
ENDDO.
Rob
10-18-2008 9:51 AM
Hello,
If you are using 1 or 3 conditions i prefer berrer using IF and ENDIF.
If you have more then 3 conditions better go for CASE......... ENDCASE.
10-31-2008 6:17 PM
hi ,
case is always much more efficent ....because while you are using if else condtion ...it will check all the condition ...untill it will get get a true condtion ...
while you are using case...it will only go to the true condtion .
Thanks and regards
Priyank dixit
10-19-2008 11:47 AM
Hi Saket,
Great discussions and great answers for your post.
I want to tell you my view too..there will not be much difference on the performance wise if you use case or if statements, its been of syntax it will be very much convenient if we use case statement when there is more searches rather if statement becomes big job validating say 20 statements.
Even when we upload records using BDC we use CASE since we can check each column and row in a sequence rather if.
Cheers!!
Balu
10-22-2008 7:16 AM
its always better to use case end case rather than if endif unless there are only 2 or 3 conditions(for which if end if is better)..
regards
palak
10-31-2008 8:44 AM
go to tcode se30-> tips & tricks -> If,Case ->If vs Case->Measure run time..
check it urself and conclude
10-31-2008 8:47 AM
This will be the docu u will get there
"Documentation
CASE statements are clearer and a little faster than
IF-constructions."
Got it...now just close this thread
10-31-2008 11:51 AM
the useless threads are the longest!
> CASE is slightly faster
> IF is more general.
The IF allows you conditions, for example < > initial etc. , which are simply not possible with the case ... endcase.
Use CASE if the conditions are simple and there are many conditions.
And learn the most important fact:
The difference are microseconds or less, the number of conditions is rarely in the hundrets, so performance-wise they ARE THE SAME! No difference which you will ever notice.
The difference between good and bad selects are seconds or even minutes, these are the points which have an performance impact.
Siegfried
10-31-2008 11:58 AM
Dear friend,
Please goto Tcode- SE30. In that click on Tips&Tricks. You will be finding entire techniues with example and measuring time.
From the examples u can understand the better one.
regards,
Prashanth maturu.
11-11-2008 5:26 AM