Batch throughput
A batch job that sorts a file and totals it by key is the workload COBOL was made for, and it is also a workload a C++ programmer writes in an afternoon. This page builds both, gives them the same file, checks that the two outputs are character for character the same, and times five runs of each on this machine. The numbers are small enough to quote and large enough to mean something.
The task and the file
The input is 200,000 records of the thirty-seven byte layout of the previous page: an id, a name, one of four regions and an amount in cents. A generator writes it, so the file is reproducible:
#include <cstdio>
#include <cstdlib>
int main(int argc, char** argv) {
long n = argc > 1 ? std::atol(argv[1]) : 200000;
std::FILE* out = std::fopen("records.dat", "w");
if (out == nullptr) {
std::perror("records.dat");
return 1;
}
const char* regions[] = {"N1", "S2", "E3", "W4"};
for (long i = 0; i < n; ++i) {
int region = static_cast<int>(i % 4);
long amount = (i * 37) % 100000;
std::fprintf(out, "C%05ld%-20s%s%09ld\n", i % 100000, "CUSTOMER", regions[region], amount);
}
std::fclose(out);
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o gen gen.cpp
./gen 200000
wc -l records.dat
wc -c records.dat
200000 records.dat
7600000 records.dat
Seven million six hundred thousand bytes: 200,000 times 37 plus a newline per record. Both jobs do the same four things with it: read every record, sort by region and then by id, write the sorted file, read the sorted file back and print a subtotal per region and a grand total. The sorted file is part of both runs, so neither language is credited for skipping the write.
The COBOL job
The sort is the SORT verb with USING and GIVING: the runtime reads the input file, sorts the records in its work file and writes the output, and the program never holds a record in memory. The aggregation is a control break over the sorted file:
IDENTIFICATION DIVISION.
PROGRAM-ID. SORTJOB.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-F ASSIGN TO "records.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-IN-STATUS.
SELECT SORT-F ASSIGN TO "sort.tmp".
SELECT OUT-F ASSIGN TO "sorted.dat"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-OUT-STATUS.
DATA DIVISION.
FILE SECTION.
FD IN-F.
01 IN-REC PIC X(37).
SD SORT-F.
01 SORT-REC.
05 S-ID PIC X(6).
05 S-NAME PIC X(20).
05 S-REGION PIC X(2).
05 S-AMOUNT PIC 9(7)V99.
FD OUT-F.
01 OUT-REC.
05 O-ID PIC X(6).
05 O-NAME PIC X(20).
05 O-REGION PIC X(2).
05 O-AMOUNT PIC 9(7)V99.
WORKING-STORAGE SECTION.
01 WS-IN-STATUS PIC XX.
01 WS-OUT-STATUS PIC XX.
01 WS-EOF PIC X VALUE "N".
01 WS-LAST PIC X(2) VALUE SPACES.
01 WS-COUNT PIC 9(7) VALUE 0.
01 WS-GROUP PIC 9(7) VALUE 0.
01 WS-SUM PIC 9(13)V99 VALUE 0.
01 WS-GROUP-SUM PIC 9(13)V99 VALUE 0.
01 WS-GRAND PIC 9(13)V99 VALUE 0.
01 WS-GROUP-OUT PIC Z(6)9.
01 WS-SUM-OUT PIC Z(12)9.99.
01 WS-GRAND-OUT PIC Z(12)9.99.
PROCEDURE DIVISION.
MAIN-PARA.
OPEN INPUT IN-F
IF WS-IN-STATUS NOT = "00"
DISPLAY "records.dat missing: run the generator" UPON STDERR
MOVE 8 TO RETURN-CODE
STOP RUN
END-IF
CLOSE IN-F
SORT SORT-F
ON ASCENDING KEY S-REGION
ON ASCENDING KEY S-ID
USING IN-F
GIVING OUT-F
OPEN INPUT OUT-F
PERFORM UNTIL WS-EOF = "Y"
READ OUT-F
AT END MOVE "Y" TO WS-EOF
NOT AT END
PERFORM HANDLE-RECORD
END-READ
END-PERFORM
CLOSE OUT-F
PERFORM PRINT-GROUP
MOVE WS-GRAND TO WS-GRAND-OUT
DISPLAY "grand total " FUNCTION TRIM(WS-GRAND-OUT)
STOP RUN.
HANDLE-RECORD.
IF O-REGION NOT = WS-LAST
PERFORM PRINT-GROUP
MOVE O-REGION TO WS-LAST
END-IF
ADD 1 TO WS-COUNT
ADD 1 TO WS-GROUP
ADD O-AMOUNT TO WS-SUM
ADD O-AMOUNT TO WS-GROUP-SUM
ADD O-AMOUNT TO WS-GRAND.
PRINT-GROUP.
IF WS-LAST NOT = SPACES
MOVE WS-GROUP TO WS-GROUP-OUT
MOVE WS-GROUP-SUM TO WS-SUM-OUT
DISPLAY "region " WS-LAST " count " FUNCTION TRIM(WS-GROUP-OUT)
" total " FUNCTION TRIM(WS-SUM-OUT)
MOVE 0 TO WS-GROUP
MOVE 0 TO WS-GROUP-SUM
END-IF.
cobc -x -free -Wall -O2 -o sortjob sortjob.cob
./sortjob
echo $?
region E3 count 50000 total 25000000.00
region N1 count 50000 total 24999000.00
region S2 count 50000 total 24999500.00
region W4 count 50000 total 25000500.00
grand total 99999000.00
0
The job checks the status of its input before it sorts anything, so a run without the file is a message and status 8 and not an empty report:
./sortjob
echo $?
records.dat missing: run the generator
8
The C++ job
The C++ version reads the whole file into a std::vector<std::string>, sorts it with a comparator that compares the region bytes at offset 26 and then the id bytes at offset 0, writes the sorted file and re-reads it for the totals. It is the straightforward C++ answer; a production version would parse the amounts into integers once and sort a vector of structs, which would move the time further, and that is noted below.
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <string>
#include <vector>
struct Record {
std::string line;
};
int main() {
std::ifstream in("records.dat");
if (!in) {
std::fprintf(stderr, "records.dat missing: run the generator\n");
return 8;
}
std::vector<std::string> records;
std::string line;
while (std::getline(in, line)) {
records.push_back(line);
}
in.close();
std::sort(records.begin(), records.end(), [](const std::string& a, const std::string& b) {
if (a.compare(26, 2, b, 26, 2) != 0) {
return a.compare(26, 2, b, 26, 2) < 0;
}
return a.compare(0, 6, b, 0, 6) < 0;
});
std::ofstream out("sorted.dat");
for (const std::string& record : records) {
out << record << '\n';
}
out.close();
std::ifstream sorted("sorted.dat");
std::string region;
std::string last;
long long count = 0;
long long group = 0;
long long sum = 0;
long long group_sum = 0;
while (std::getline(sorted, line)) {
region = line.substr(26, 2);
long long amount = std::stoll(line.substr(28, 9));
if (region != last) {
if (!last.empty()) {
std::printf("region %s count %lld total %lld.%02lld\n", last.c_str(), group,
group_sum / 100, group_sum % 100);
group = 0;
group_sum = 0;
}
last = region;
}
++count;
++group;
sum += amount;
group_sum += amount;
}
if (!last.empty()) {
std::printf("region %s count %lld total %lld.%02lld\n", last.c_str(), group,
group_sum / 100, group_sum % 100);
}
std::printf("grand total %lld.%02lld\n", sum / 100, sum % 100);
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o sortjob_cpp sortjob.cpp
./sortjob_cpp
echo $?
region E3 count 50000 total 25000000.00
region N1 count 50000 total 24999000.00
region S2 count 50000 total 24999500.00
region W4 count 50000 total 25000500.00
grand total 99999000.00
0
The two outputs are identical. That is the first requirement of a comparison and it is checked rather than assumed: the totals add up, the counts add up to 200,000, and a diff of the two outputs is empty.
The timing method
Wall time of the whole process, measured with the shell, one warm-up run of each program first, then five runs of each with the two programs alternating so that neither one always runs on a colder cache:
./sortjob > /dev/null && ./sortjob_cpp > /dev/null
for i in 1 2 3 4 5; do for p in ./sortjob_cpp ./sortjob; do \
s=$(date +%s%N); $p > /dev/null; e=$(date +%s%N); \
printf "%s %s\n" "$p" "$(( (e - s) / 1000000 ))"; done; done
./sortjob_cpp 421
./sortjob 822
./sortjob_cpp 440
./sortjob 803
./sortjob_cpp 343
./sortjob 889
./sortjob_cpp 332
./sortjob 1211
./sortjob_cpp 568
./sortjob 854
The numbers
| Program | Runs, ms | Minimum | Median |
|---|---|---|---|
| COBOL sortjob | 822, 803, 889, 1211, 854 | 803 | 854 |
| C++ sortjob_cpp | 421, 440, 343, 332, 568 | 332 | 421 |
By the minimum the C++ job took 332 milliseconds and the COBOL job 803, a factor of 2.4; by the median 421 against 854, a factor of 2.0. The COBOL set has one run at 1211 milliseconds, nearly half again as long as its neighbour, which is the machine and not the program: this container shares a host, has four cores, and neither program uses more than one of them.
Reading the numbers honestly
The C++ job is faster here by about a factor of two, and the reasons are worth naming before anyone quotes the number. The C++ version compares two short substrings in a comparator that the compiler inlines; the COBOL version calls the runtime sort, which compares the declared keys through the generated C and writes more data through its own buffering. The C++ version also fits the whole file in memory, which the COBOL runtime also does for this size and might not for a file of a hundred gigabytes, where its work file is on disk and the C++ program would have to be rewritten.
What the comparison does not measure: the cost of writing either program, the cost of changing it (next page but one), the time a COBOL program spends compiling against a C++ program (both are a second or two), and the I/O of a real mainframe, which has neither this file system nor this page cache. A production C++ job would parse each record once into a struct of integers, sort that, and gain more; a production COBOL job would do exactly what this one does. The factor of two is real on this machine for this shape of job, and it is not a law.
The COBOL lessons that build this job are SORT with USING and GIVING, the control-break report and the pipeline capstone. The next page leaves throughput and compares the part of the output a person reads: edited pictures against std::format.
The COBOL was built with cobc (GnuCOBOL) 3.2.0 and the C++ with g++ (Alpine 15.2.0) 15.2.0, both with -O2, and timed with the shell on Alpine Linux 3.24.3 (x86_64) in a Docker container with nproc 4. The input file was 7,600,000 bytes; the machine is shared and the numbers are wall time of this container, not of a dedicated server.
Article author: Arthur Isaev