Discussion: Shortest way to add special character



Discussion: Shortest way to add special character

Postby Aki88 » Thu Jul 21, 2016 7:52 pm

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.
Aki88
 
Posts: 371
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 33 times
Been thanked: 35 times

Re: Discussion: Shortest way to add special character

 

Re: Discussion: Shortest way to add special character

Postby enrico-sorichetti » Thu Jul 21, 2016 8:14 pm

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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2636
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 129 times

Re: Discussion: Shortest way to add special character

Postby Aki88 » Thu Jul 21, 2016 8:38 pm

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.
Aki88
 
Posts: 371
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 33 times
Been thanked: 35 times

Re: Discussion: Shortest way to add special character

Postby Terry Heinze » Thu Jul 21, 2016 8:53 pm

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.
.... Terry
Terry Heinze
 
Posts: 190
Joined: Wed Dec 04, 2013 11:08 pm
Location: Richfield, MN, USA
Has thanked: 10 times
Been thanked: 11 times

Re: Discussion: Shortest way to add special character

Postby Aki88 » Thu Jul 21, 2016 9:12 pm

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.
Aki88
 
Posts: 371
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 33 times
Been thanked: 35 times

Re: Discussion: Shortest way to add special character

Postby Robert Sample » Thu Jul 21, 2016 9:39 pm

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.
Robert Sample
Global moderator
 
Posts: 3363
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 221 times

Re: Discussion: Shortest way to add special character

Postby Robert Sample » Thu Jul 21, 2016 10:35 pm

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.
Robert Sample
Global moderator
 
Posts: 3363
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 221 times

Re: Discussion: Shortest way to add special character

Postby Aki88 » Fri Jul 22, 2016 12:16 am

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.
Aki88
 
Posts: 371
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 33 times
Been thanked: 35 times

Re: Discussion: Shortest way to add special character

Postby BillyBoyo » Fri Jul 22, 2016 12:19 am


     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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times


Return to Stupid Questions

 


  • Related topics
    Replies
    Views
    Last post