Exercises: Performance Tuning
Exercise 36.1: Data Type Performance Comparison (Beginner)
Write three versions of a program that computes simple interest (P * r * t / 365) in a loop of 1,000,000 iterations:
- Version A: All fields as DISPLAY (PIC 9(9)V99)
- Version B: All fields as COMP-3 (PIC 9(9)V99 COMP-3)
- Version C: All fields as COMP (PIC S9(9)V99 COMP)
Use FUNCTION CURRENT-DATE before and after the loop to measure elapsed time. Record the results in a table and calculate the speedup factor for each version relative to DISPLAY.
Exercise 36.2: SEARCH vs. SEARCH ALL (Beginner)
Create a COBOL program with a sorted table of 1,000 entries (key: PIC 9(4), value: PIC X(20)). Write two versions of a lookup routine:
- Version A: Uses SEARCH (linear)
- Version B: Uses SEARCH ALL (binary)
Perform 100,000 lookups using random keys. Time both versions. What is the speedup factor? Now try with a 10,000-entry table. How does the speedup change?
Exercise 36.3: Block Size Analysis (Beginner)
A sequential file has 5,000,000 records of 200 bytes each. Calculate the number of I/O operations required to read the entire file for each of these block sizes:
| Block Size | Records per Block | I/O Operations | Estimated I/O Time (at 5ms/IO) |
|---|---|---|---|
| 200 | |||
| 2,000 | |||
| 8,000 | |||
| 16,000 | |||
| 32,000 |
Which block size would you recommend? What factors might limit your choice?
Exercise 36.4: Loop Optimization (Intermediate)
The following loop processes 2,000,000 records. Identify three performance problems and rewrite the loop to be more efficient. Estimate the speedup for each change.
2000-PROCESS-ALL.
PERFORM VARYING WS-IDX FROM 1 BY 1
UNTIL WS-IDX > 2000000
MOVE FUNCTION CURRENT-DATE TO WS-TIMESTAMP
PERFORM 2100-FORMAT-DATE
SEARCH WS-RATE-TABLE
AT END MOVE 0 TO WS-RATE
WHEN WS-RATE-CODE(WS-RT-IDX)
= WS-ACCT-TYPE(WS-IDX)
MOVE WS-RATE-VALUE(WS-RT-IDX)
TO WS-RATE
END-SEARCH
COMPUTE WS-INTEREST(WS-IDX) =
WS-BALANCE(WS-IDX) * WS-RATE / 365
* WS-DAYS
IF WS-INTEREST(WS-IDX) > 0
COMPUTE WS-TAX(WS-IDX) =
WS-INTEREST(WS-IDX) * 0.30
ELSE
MOVE 0 TO WS-TAX(WS-IDX)
END-IF
END-PERFORM.
Exercise 36.5: SQL Optimization (Intermediate)
Rewrite the following embedded SQL to eliminate the "SQL in a loop" anti-pattern. The original code processes 10,000 claims:
PERFORM VARYING WS-I FROM 1 BY 1
UNTIL WS-I > WS-CLAIM-COUNT
EXEC SQL
SELECT PROVIDER_NAME, PROVIDER_ADDR
INTO :WS-PROV-NAME, :WS-PROV-ADDR
FROM PROVIDERS
WHERE PROVIDER_ID = :WS-PROV-ID(WS-I)
END-EXEC
EXEC SQL
SELECT MEMBER_NAME
INTO :WS-MBR-NAME
FROM MEMBERS
WHERE MEMBER_ID = :WS-MBR-ID(WS-I)
END-EXEC
PERFORM 3000-FORMAT-OUTPUT
END-PERFORM
Provide: 1. A rewritten version using a single cursor with JOINs 2. An estimate of the I/O reduction (how many DB2 calls are saved?) 3. Index recommendations for the query
Exercise 36.6: Compiler Option Analysis (Intermediate)
For each of the following scenarios, recommend the appropriate compiler options and explain your reasoning:
- A development environment where debugging ease is the priority
- A production batch job processing 10 million records nightly
- A CICS online transaction that must respond in under 200ms
- A production program that processes external data from third-party sources (data quality unknown)
Consider: OPTIMIZE, NUMPROC, TRUNC, SSRANGE, and TEST.
Exercise 36.7: Working-Storage Layout Optimization (Intermediate)
Reorganize the following WORKING-STORAGE section to minimize slack bytes. Calculate the storage savings.
01 WS-TRANSACTION.
05 WS-TXN-FLAG PIC X.
05 WS-TXN-AMOUNT PIC S9(9)V99 COMP.
05 WS-TXN-CODE PIC XX.
05 WS-TXN-COUNT PIC S9(9) COMP.
05 WS-TXN-TYPE PIC X.
05 WS-TXN-RATE PIC S9(3)V9(6) COMP.
05 WS-TXN-STATUS PIC XXX.
05 WS-TXN-TOTAL PIC S9(13)V99 COMP.
Exercise 36.8: Performance Tuning Case Study (Advanced)
A batch program processes a master file (VSAM KSDS, 3 million records, 500 bytes each) against a transaction file (sequential, 200,000 records, 100 bytes each). For each transaction, the program:
- Reads the transaction sequentially
- Reads the corresponding master record randomly (by key)
- Performs a calculation (5 arithmetic operations)
- Updates the master record
- Writes a report line
Current performance: 45 minutes elapsed, 8 minutes CPU. The target is 15 minutes elapsed.
Design a complete optimization strategy: - Identify whether the program is CPU-bound or I/O-bound - Propose specific optimizations (with estimated time savings for each) - Recommend JCL changes - Recommend compiler option changes - Explain any trade-offs
Exercise 36.9: Caching Strategy Design (Advanced)
Design an in-memory caching strategy for a COBOL program that processes claims and needs to look up fee schedules from a VSAM file. There are 50,000 unique fee schedule entries. The program processes 100,000 claims per run, with 80% of claims hitting the top 500 fee schedule entries.
- Design the cache table structure (WORKING-STORAGE layout)
- Choose between SEARCH and SEARCH ALL for cache lookups (justify)
- Design the cache-miss logic (what happens when the entry isn't cached)
- Calculate the expected cache hit rate
- Estimate the I/O savings compared to looking up every claim