问题
Which is better in terms of performance ?
"if else
" or "evaluate
" statement in COBOL, when we have lesser conditons to check ?
回答1:
It seems we have a doubter as an OP, so here's an example with IBM Enterprise COBOL:
01 A PIC 9.
PROCEDURE DIVISION.
ACCEPT A
IF A EQUAL TO 2
CONTINUE
ELSE
CONTINUE
END-IF
EVALUATE A
WHEN 2
CONTINUE
WHEN OTHER
CONTINUE
END-EVALUATE
And here is the code which is generated. You don't need to know IBM Mainframe Assembler, you just have to note that things are the same:
000008 IF
0002F8 D200 D0F8 8000 MVC 248(1,13),0(8) TS2=0
0002FE 96F0 D0F8 OI 248(13),X'F0' TS2=0
000302 95F2 D0F8 CLI 248(13),X'F2' TS2=0
000306 4770 B126 BC 7,294(0,11) GN=4(00030E)
000009 CONTINUE
00030A 47F0 B126 BC 15,294(0,11) GN=5(00030E)
00030E GN=4 EQU *
000011 CONTINUE
00030E GN=5 EQU *
000013 EVALUATE
000014 WHEN
00030E D200 D0F8 8000 MVC 248(1,13),0(8) TS2=0
000314 96F0 D0F8 OI 248(13),X'F0' TS2=0
000318 95F2 D0F8 CLI 248(13),X'F2' TS2=0
00031C 4770 B13C BC 7,316(0,11) GN=12(000324)
000015 CONTINUE
000320 47F0 B13C BC 15,316(0,11) GN=11(000324)
000324 GN=12 EQU *
000016 WHEN
000017 CONTINUE
000324 GN=11 EQU *
CONTINUE, which generates no instructions, is used to keep things simple for both "legs" in the IF and the EVALUATE.
There is no reason to believe that the code generated by any non-IBM compiler would differ for the two examples.
If you don't even have the energy to respond to questions or make comments if you are unclear, then don't expect too much in the future when you do summon up the energy to ask a question.
Back to the original...
If you have a performance problem in a COBOL program it is highly unlikely to be down to the use of IF or EVALUATE per se.
EVALUATE can be used as a direct replacement for a nested-IF.
If you find an old nested-IF it will look something like this:
IF A
do something
ELSE
IF B
do something
ELSE
IF C
do something
ELSE
IF D
do something
ELSE
IF E
do something.
Or, like this:
IF A
do something
ELSE
IF B
do something
ELSE
IF C
do something
ELSE
IF D
do something
ELSE
IF E
do something.
You can know it is old code, because of the full-stop/period.
Nested-IFs in new code are much better as EVALUATE.
There should be no real overlap between their usage, but it is not problematic from a performance point of view if there is.
I don't know about all compilers, but I would not be surprised if an IF/ELSE generates identical code to a simple EVALUATE/WHEN/WHEN OTHER.
If you have a performance problem, look to your logic.
If you are thinking of just "optimising" a COBOL program to make it somehow better, forget about it. Get the logic straight and understandable. Make the program maintainable.
If you want to know about making your programs run a little faster (and the emphasis may be on a little) then write some test programs with some manipulations of numeric fields of different types (USAGE DISPLAY vs PACKED-DECIMAL/COMP-3 vs BINARY/COMP/various other non-floating-point COMP options provided by your compiler).
Check them out with subscripting, counting, accumulating values with decimal places, computations with multiple sources. Then you'll know how to define your fields for the purpose that they have, and won't have to consider it afterwards.
If you have site standards where you are, use them.
Don't just write a COBOL programs and then sit down once they are working and say "now to optimise it". It's not what we do.
回答2:
The differences are largely irrelevant. Premature optimization is usually not a good idea.
Your compiler will optimize both at compile time. At runtime, your processor will cache the most highly executed code or predictively execute likely paths.
The way to approach writing source code is to write for the human maintenance programmers that will come after you and will have to look at said program at 0300 when it blows up in production. Make it as clear as possible what the code is doing. If that means using "IF" instead of "EVALUATE", do that, write for human readability.
Only when you get into production and you have profiled your code with real world data should you be considering tweaking for performance.
来源:https://stackoverflow.com/questions/22627634/which-is-better-in-terms-of-performance-if-else-or-evaluate-statement-in-co