Page 1 of 1
Discussion: Shortest way to add special character
Posted:
Thu Jul 21, 2016 7:52 pm
by Aki88
Hello,
Out of curiosity, for instance we have:
Variable A: 011190
Expected result: 01.11.90
In COBOL, what is the shortest method to achieve the above result (in terms of instructions), is it possible to achieve it in one MOVE statement?
The simplest approaches I could think of were; but all had at least 2-3 MOVE:
a. Move Var-A to another Var with definition 99B99B99; and then INSPECT replacing the spaces with '.'
b. Move Var-A to another Var which is redefined- broken into chunks of 2 bytes each; have another variable with redefs, and '.'; then perform a MOVE CORR from the original redef to the redef with '.'.
c. UNSTRING the data
Thank you.
Re: Discussion: Shortest way to add special character
Posted:
Thu Jul 21, 2016 8:14 pm
by enrico-sorichetti
the PROPER method ( the number of instructions is irrelevant ) is to use a sequence of instructions easily understandable and maintainable .
if You are curious just write a short program with the different approaches and look at the assembler instructions generated
Re: Discussion: Shortest way to add special character
Posted:
Thu Jul 21, 2016 8:38 pm
by Aki88
Hello Enrico,
Thank you, looking at the LIST output is what I was doing to find the shortest instruction-path-length for the shared approaches.
Reason for asking this seemingly dumb query was a scenario, wherein there are hundreds of similar fields per record in a dataset having over a few million records.
Multiple data formatting operations are being performed on each record, which is then being written to an output DS. If there is a way of achieving the desired result in one/two MOVE statements or a less intensive operation, that is any day better than any of the approaches that I can think of currently.
Re: Discussion: Shortest way to add special character
Posted:
Thu Jul 21, 2016 8:53 pm
by Terry Heinze
If this is a one-time only project, I wouldn't spend a lot of time investigating due to the fact that you'd probably spend more time researching and testing than would be saved with today's high-speed CPUs. On the other hand, if this is a regularly occurring project, I'd go with your and enrico's suggestion of studying the LIST output to see the assembler instructions generated with each option.
Re: Discussion: Shortest way to add special character
Posted:
Thu Jul 21, 2016 9:12 pm
by Aki88
Hello Terry,
Thank you; this is a recurring piece in a few daily batch COBOL programs; the prime program runs during critical processing window with the earlier mentioned processing load. A legacy code, should I say! Current logic uses INSPECT code piece.
Re: Discussion: Shortest way to add special character
Posted:
Thu Jul 21, 2016 9:39 pm
by Robert Sample
I tested
05 WS-SOURCE PIC 9(6) VALUE 011190.
05 WS-TARGET1 PIC 99B99B99.
PROCEDURE DIVISION.
0000-MAIN.
MOVE WS-SOURCE TO WS-TARGET1.
INSPECT WS-TARGET1 REPLACING ALL SPACE BY '.'.
MOVE WS-SOURCE TO WS-TARGET1.
MOVE '.' TO WS-TARGET1 (3 : 1)
WS-TARGET1 (6 : 1).
and got generated pseudo-assembler of
00018C 000012 USER-ENTRY: EQU *
000012: 001800 MOVE WS-SOURCE
00018C D209 D168 3038 000012 MVC 360(10,R13),56(R3) # TS2=5
000192 F235 D0D8 8098 000012 PACK 216(4,R13),152(6,R8) # TS2=7 WS-SOURCE
000198 960F D0DB 000012 OI 219(,R13),X'0F' # TS2=7
00019C DE09 D168 D0D8 000012 ED 360(10,R13),216(R13) # TS2=5 TS2=7
0001A2 D207 809E D16A 000012 MVC 158(8,R8),362(R13) # WS-TARGET1 TS2=5
000014: 002000 INSPECT WS-TARGET1
0001A8 DC07 809E 30A0 000014 TR 158(8,R8),160(R3) # _$CONSTANT_AREA+160
000016: 002300 MOVE WS-SOURCE
0001AE D209 D168 3038 000016 MVC 360(10,R13),56(R3) # TS2=5
0001B4 F235 D0D8 8098 000016 PACK 216(4,R13),152(6,R8) # TS2=7 WS-SOURCE
0001BA 960F D0DB 000016 OI 219(,R13),X'0F' # TS2=7
0001BE DE09 D168 D0D8 000016 ED 360(10,R13),216(R13) # TS2=5 TS2=7
0001C4 D207 809E D16A 000016 MVC 158(8,R8),362(R13) # WS-TARGET1 TS2=5
000018: 002320 MOVE '.'
0001CA D200 80A0 3046 000018 MVC 160(1,R8),70(R3) #
0001D0 D200 80A3 3046 000018 MVC 163(1,R8),70(R3) #
The two MVI instructions might be a little quicker than the TR used by INSPECT, but even if so I doubt the difference is going to be a huge amount even over millions of records.
Re: Discussion: Shortest way to add special character
Posted:
Thu Jul 21, 2016 10:35 pm
by Robert Sample
Update: I ran a test of each method by executing the two statements in a PERFORM loop for 10,000,000 times. INSPECT used 186,038 service units and reference modification used 128,052 service units.
Re: Discussion: Shortest way to add special character
Posted:
Fri Jul 22, 2016 12:16 am
by Aki88
Robert Sample wrote:Update: I ran a test of each method by executing the two statements in a PERFORM loop for 10,000,000 times. INSPECT used 186,038 service units and reference modification used 128,052 service units.
Thank you Robert, this was really-really helpful. This is what I had on my mind, that once the number of records are
increased the INSPECT will be the one which is costlier. But I wasn't sure by how much; as it might seem, unless the input is
really high, they are almost at par.
Re: Discussion: Shortest way to add special character
Posted:
Fri Jul 22, 2016 12:19 am
by BillyBoyo
05 WS-SOURCE PIC 9(6) VALUE 011190.
05 FILLER
REDEFINES WS-SOURCE.
10 WS-S-DD PIC XX.
10 WS-S-MM PIC XX.
10 WS-S-YY PIC XX.
05 WS-TARGET1.
10 WS-T-DD PIC XX.
10 FILLER PIC X VALUE ".".
10 WS-T-MM PIC XX.
10 FILLER PIC X VALUE ".".
10 WS-T-YY PIC XX.
MOVE WS-S-DD TO WS-T-DD
MOVE WS-S-MM TO WS-T-MM
MOVE WS-S-YY TO WS-T-YY
You'll find a lot of the generated code magically disappears. You won't get faster.
Often, more code, more definitions, less generated code. That'll be three MVC's (before V5, anyway) and nothing else. Removes MVC of six bytes, PACK, OI, ED (which is slow, apparently, V6 at least has better code) just though not using the "short" way of editing it.
Robert, the "two MVIs" were MVCs. Interesting.