You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: COBOL Programming Course #1 - Getting Started/COBOL Programming Course #1 - Getting Started.md
+168Lines changed: 168 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4278,3 +4278,171 @@ Previous lab programs made use of a date/time intrinsic function. The date/time
4278
4278
**Lab Hints**
4279
4279
4280
4280
Refer to CBL0011 line 120 for the proper formatting of the function-name causing the compile error.
4281
+
4282
+
\newpage
4283
+
4284
+
# Program tuning and simplification
4285
+
4286
+
In the previous chapters, we have seen how you could code COBOL applications. But now, let us explore how to improve them.
4287
+
4288
+
When a program is comprehensible, we can assess its performance. However, if the opposite is true, it can make your application difficult to understand and maintain, thus hindering optimization.
4289
+
4290
+
To improve performance, we should take note of the following things:
4291
+
4292
+
- The underlying algorithm of your business logic
4293
+
- Data structure
4294
+
4295
+
Having a robust algorithm with the appropriate data structure is essential to improve performance.
4296
+
4297
+
We can also write programs that result in more efficient use of the available services. We can also use coding techniques to improve our productivity.
4298
+
4299
+
If you are interested in learning more about performance tuning with COBOL, check out the [Enterprise COBOL for z/OS Performance Tuning Guide](http://publibfp.dhe.ibm.com/epubs/pdf/igy6tg30.pdf).
4300
+
4301
+
## Optimal programming style
4302
+
4303
+
Enterprise COBOL came with an in-build optimizer, and the coding style we use can affect how it handles our code. We can improve optimization through the use of structured programming techniques, factoring expressions, using symbolic constants or grouping constant and duplicate computations.
4304
+
4305
+
### Using structured programming
4306
+
4307
+
Using structured programming statements, such as EVALUATE and inline PERFORM, can make our program more comprehensible and generates a more linear control flow, which enables the optimizer to produce a more efficient code.
4308
+
4309
+
We can also use top-down programming constructs. In simpler term, we would work with a very general overview of what our program should do, before using that to build upon the required operations. In COBOL, out-of-line PERFORM statements are a natural way of doing top-down programming. It can be as efficient as an inline PERFORM because the compiler can simplify or remove the linkage code.
4310
+
4311
+
Before we continue, let us talk a bit about in-line and out-of-line PERFORM statements. Chances are you have seen them without realizing what they are. Take a look at the example below:
4312
+
4313
+
```
4314
+
PERFORM 010-INITIALIZE
4315
+
PERFORM UNTIL END-OF-FILE
4316
+
READ FILE-DATA INTO WS-DATA
4317
+
AT END
4318
+
SET END-OF-FILE TO TRUE
4319
+
NOT AT END
4320
+
PERFORM 020-UPDATE-TRANSACTION
4321
+
END-READ
4322
+
END-PERFORM
4323
+
```
4324
+
4325
+
In the example above, we have two out-of-line PERFORM and one inline PERFORM. An inline PERFORM is executed in the normal flow of a program, while an out-of-line PERFORM will branch to the named paragraph.
4326
+
4327
+
It is also suggested to avoid the use of the following constructs:
4328
+
4329
+
- ALTER statements
4330
+
- Explicit GO TO statements
4331
+
- PERFORM procedures that involve irregular control flow
4332
+
4333
+
### Factoring expressions
4334
+
4335
+
We can also factor expressions in our programs to eliminate unnecessary computation. Take a look at the examples below. The first block of code is more efficient than the second block of code.
4336
+
4337
+
```
4338
+
MOVE ZERO TO TOTAL
4339
+
PERFORM VARYING I FROM 1 BY 1 UNTIL I = 10
4340
+
COMPUTE TOTAL = TOTAL + ITEM(I)
4341
+
END-PERFORM
4342
+
COMPUTE TOTAL = TOTAL * DISCOUNT
4343
+
```
4344
+
4345
+
```
4346
+
MOVE ZERO TO TOTAL
4347
+
PERFORM VARYING I FROM 1 BY 1 UNTIL I = 10
4348
+
COMPUTE TOTAL = TOTAL + ITEM(I) * DISCOUNT
4349
+
END-PERFORM
4350
+
```
4351
+
4352
+
### Using symbolic constants
4353
+
4354
+
If we have a data item that is constant throughout the program, we can initialize it with a VALUE clause and not change it anywhere in the program.
4355
+
4356
+
However, if we pass a data item to a subprogram BY REFERENCE, the optimizer will treat it as an external data item and assumes that it is changed at every subprogram call.
4357
+
4358
+
## Choosing efficient data types
4359
+
4360
+
The use of consistent data types can reduce the need for conversions. We can also carefully determine when to use fixed-point and floating-point data types to improve performance.
4361
+
4362
+
### Efficient computational data types
4363
+
4364
+
When we use a data item mainly for arithmetic or subscripting purposes, we can code USAGE BINARY on the data description. The operations to manipulate binary data are faster than those for decimal data.
4365
+
4366
+
However, when we are dealing with an intermediate result with a large precision, the compiler will use decimal arithmetic. Normally, for fixed-point arithmetic statements, the compiler will use binary arithmetics for precision of eight or fewer digits. Anything above 18 digits will always be computed using decimal arithmetics, and those in-between can use either form.
4367
+
4368
+
Therefore, to produce the most efficient code for a BINARY data item, we need to ensure that it has a sign (indicated with an S in the PICTURE clause) and eight or fewer digits.
4369
+
4370
+
But for a data item that is larger than eight digits or is used with DISPLAY or NATIONAL data items, we can use PACKED-DECIMAL. The code generated can be as fast as BINARY data items in some cases, especially if the statement is complicated or involves rounding.
4371
+
4372
+
To produce the most efficient code for a PACKED-DECIMAL data item, we need to ensure it has a sign (indicated with an S in the PICTURE clause), an odd number of digits, and 15 or fewer digits in the PICTURE clause (since the instructions the compiler use are faster with 15 or fewer digits).
4373
+
4374
+
### Consistent data types
4375
+
4376
+
In operations with operands of different types, one of the operands will be converted to the same type as the other. This would require several instructions.
4377
+
4378
+
Therefore, to improve performance, we can avoid conversions by using consistent data types and by giving both operands the same usage and appropriate PICTURE specifications.
4379
+
4380
+
### Efficient arithmetic expressions
4381
+
4382
+
Computation of arithmetic expressions that are evaluated in floating point is most efficient when little or no conversion is involved. We can use operands that are COMP-1 or COMP-2 to produce the most efficient code.
4383
+
4384
+
We can also define integer items as BINARY or PACKED-DECIMAL with nine or fewer digits to enable quick conversion to floating-point data. Note that conversion from COMP-1 or COMP-2 to a fixed-point integer with nine or fewer digits is efficient when the value of the COMP-1 or COMP-2 item is less than 1,000,000,000.
4385
+
4386
+
### Efficient exponentiations
4387
+
4388
+
We can use floating point for exponentiations for large exponents to achieve faster and more accurate results. For example, the first statement below is computed more quickly and accurately compared with the second statement:
By using floating-point exponent, the compiler will use floating-point arithmetics to compute the exponentiations.
4396
+
4397
+
## Handling tables efficiently
4398
+
4399
+
We can also use several techniques to improve the efficiency of your table-handling applications.
4400
+
4401
+
To refer to table elements efficiently, we can:
4402
+
4403
+
-**Use indexing rather than subscripting.** Since the value of the index is already has the element size factored into it, preventing any need in calculating during run time.
4404
+
4405
+
-**Use relative indexing.** Relative index references can be executed at least as fast as direct index references, and sometimes faster.
4406
+
4407
+
Regardless of how you reference your table elements, we can also:
4408
+
4409
+
-**Specify the element length to match that of related tables.** This will enable the optimizer to reuse the index or subscript computed for one table.
4410
+
4411
+
-**Avoid errors in reference by coding index and subscript checks into your program.**
4412
+
4413
+
We can also improve the efficiency of tables by:
4414
+
4415
+
- Using binary data items for all subscripts
4416
+
- Using binary data items for variable-length table items
4417
+
- Using fixed-length data items whenever possible
4418
+
- Organize tables according to the type of search method used
4419
+
4420
+
## Eliminating repetitive coding
4421
+
4422
+
If your program contains frequently used code sequences, we can write the code sequence once and put them in a COBOL copy library. These code sequences are referred to as copybooks. Then, we can use the COPY statement to retrieve these code sequences and include them during compile time. Using copybooks in this manner will eliminate repetitive coding.
4423
+
4424
+
We would need to specify a copy library in the JCL we used. If you are using the provided procedure (IGYWC, IGYWCL or IGYWCLG), you can supply a DD statement to the COBOL step inside the procedure. For example:
4425
+
4426
+
```
4427
+
//COBOL.SYSLIB DD DISP=SHR,DSN=Z99998.COPYLIB
4428
+
```
4429
+
4430
+
This will tell the compiler to look for the copybooks on the supplied dataset.
4431
+
4432
+
Let us take a look at an example of how we can use the COPY statement in a program.
4433
+
4434
+
Assume that a copybook with the name of DOWORK is stored and contains the following statement:
4435
+
```
4436
+
COMPUTE SPACE-AVAILABLE = TOTAL-FREE-SPACE
4437
+
MOVE SPACE-AVAILABLE TO PRINT-SPACE
4438
+
```
4439
+
4440
+
We can retrieve the copybook by coding:
4441
+
```
4442
+
PROCEDURE DIVISION.
4443
+
...
4444
+
DISPLAY "RETRIEVE COPYBOOK".
4445
+
COPY DOWORK.
4446
+
```
4447
+
4448
+
The statements inside the DOWORK procedure will then follows the DISPLAY statement.
0 commit comments