INSPECT VARIABLE1 (1)
'0 AB ' BY '0TH AB '
'1 AB ' BY '1ST AB '
'2 AB ' BY '2ND AB '
'3 AB ' BY '3RD AB '
'4 AB ' BY '4TH AB '
'5 AB ' BY '5TH AB '
'6 AB ' BY '6TH AB '
'7 AB ' BY '7TH AB '
'8 AB ' BY '8TH AB '
'9 AB ' BY '9TH AB '
The above code, Its consuming more CPU time so I want to reduce CPU time.
One thing you can do is move var (1) to wrk-var and inspect wrk-var. Some O'head is generated to calc the position of var (1). This approach is usually used when multiple operations are performed on a table entry. It may not save much compute time here, but may be worth a try.
BTW, the manual says that the subject and substitution fields must be the same length. I'm surprised you're not getting a compiler error, unless the spaces in your literals are not showing and they equalize the lengths.
Joined: 14 Jan 2008 Posts: 2504 Location: Atlanta, Georgia, USA
As Jack has said, move the variable to another WS area, but then, change your INSPECT REPLACING to INSPECT CONVERTING and issue the INSPECT CONVERTING 10 consecutive times, each time changing the FROM and TO literals.
This will generate 10 in-line "TR" (Translate) instructions (when the length of the variable is less than 257) and the overall CPU of these 10 Translate instructions will be less than the CPU generated by the method with which you're using currently.
INSPECT REPLACING will always cause COBOL to CALL (BALR) to a run-time routine and (IMHO) this is where your CPU overhead is occurring.
Try the INSPECT CONVERTING with literals and see if this reduces the CPU. I have a feeling that it will, even though you're issuing 10 INSPECT's, sometimes more is better.
03 WS-VARIABLE PIC X(nnn).
MOVE VARIABLE (1) TO WS-VARIABLE.
INSPECT WS-VARIABLE CONVERTING '0 AB ' TO '0TH AB '
INSPECT WS-VARIABLE CONVERTING '1 AB ' TO '1ST AB '
Just add your INSPECT's for '3 AB' through '9 AB'.
To reemphasize, COBOL will resolve INSPECT CONVERTING with LITERALS to execute in-line, never leaving the program.
Note that if the target variable's length is greater than 256 but less than 1025, COBOL will issue up to four (4) "TR" instructions, each with a maximum length of 256, which is the maximum length for the first operand (in this case, WS-VARIABLE) of a single "TR" instruction. If the target variable's length exceeds 1024, then COBOL will unfortunately, BALR to a COBOL run-time routine and defeat the purpose of reducing CPU. If the target variable's length exceeds 1024, then carve the variable into individual 1024-byte chunks.