, 4 min read

Comparing GnuCOBOL to IBM COBOL

Original post is here eklausmeier.goip.de/blog/2021/10-05-comparing-gnucobol-to-ibm-cobol.


When I ran the n-queens problem in COBOL using GnuCOBOL I was a little bit surprised how slow the resulting program was -- it was slower than the equivalent PHP program. Therefore I installed the IBM COBOL for Linux compiler on the same machine and compared performance, see Installing IBM COBOL for Linux on Arch Linux. The IBM compiler generated program was 17-times faster!

Here are the results in seconds (real) on an Intel NUC.

Program runtime
GnuCOBOL 6.33
IBM COBOL 0.37

I reproduce the previous results Performance Comparison C vs. Java vs. Javascript vs. LuaJIT vs. PyPy vs. PHP vs. Python vs. Perl:

Language NUC Ryzen Odroid
C 0.17 0.15 0.44
Java 1.8.0 0.31 0.22 108.17
node.js 16.4 0.34 0.21 1.67
LuaJIT 0.49 0.33 2.06
PyPy3 7.3.5 1.57 0.86 n/a
PHP 8 3.23 2.35 42.38
Python 3.9.6 12.29 7.65 168.17
Perl 5.34 25.87 21.14 209.47

I compiled the COBOL program with GnuCOBOL using cobc -O2 -x, and for IBM COBOL I used cob2 -q'OPTIMIZE(FULL)'. Running the two programs is:

$ time xdamcnt 1 12

So the GnuCOBOL program is slower than almost everything except Python and Perl. While the IBM COBOL is two times slower than the C program and in the range of Java and JavaScript, but still faster than LuaJIT. The slow performance of GnuCOBOL was not expected, while the performance of IBM COBOL is inline with expectations.

Some notes on comparison:

  1. Performancewise IBM COBOL is a welcome alternative to GnuCOBOL. On the other hand IBM COBOL is 32-bit with all its shortcomings. As I remarked in Memory Limitations with IBM Enterprise COBOL Compiler: No big-data with IBM COBOL.
  2. GnuCOBOL is way more permissive with its input format than IBM COBOL, which is still very mainframe oriented.
  3. Also GnuCOBOL offers an easier way to handle command-line arguments. Command-line handling in IBM COBOL is a little bit brittle, although it is not in any way difficult.
  4. GnuCOBOL runs on ARM, while IBM COBOL is x86 only. So IBM COBOL will not run on Graviton or Ampere.

Here is the COBOL program used for the tests:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. xdamcnt.
       AUTHOR.       Elmar Klausmeier.
       DATE-WRITTEN. 03-Oct-2021.
      *
      * entworfen am 31.03.1985
      * geschrieben am 02.04.1985
      * revidiert am 18.04.1985
      * Umgeschrieben auf COBOL: 03.10.2021
      *
      *     2   4    6     8      10         12           14
      *   1 0 0 2 10 4 40 92 352 724 2680 14200 73712 365596 
      *
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       77 i             pic 9(8) comp-5.
       77 j             pic 9(8) comp-5.
       77 k             pic 9(8) comp-5.
       77 N             pic 9(8) comp-5.
       77 N2            pic 9(8) comp-5.
       77 l             pic s9(8) comp-5.
       77 z             pic 9(8) comp-5.
       77 configOKret   pic 9(8) comp-5.
       01 A_vector.
          10  A         pic 9(8) comp-5 occurs 100 value 0.
       77 istart        pic 9(8) comp-5 VALUE 1.
       77 iend          pic 9(8) comp-5 VALUE 0.
       77 cnt           pic 9(8) comp-5 VALUE 0.
       77 slen          pic 9(8) comp-5 value 0.
       77 argc          pic 9(8) comp-5 VALUE 0.
       77 argv          PIC X(100) VALUE SPACES.

       LINKAGE SECTION.
      *01 argc          pic s9(8) comp-5.
      *01 argv.
      *   02 argvTable OCCURS 1 TO 100 TIMES DEPENDING ON argc.
      *      03 pargv   pointer.
      *01 argviStr      pic x(8). 

       PROCEDURE DIVISION.
      *PROCEDURE DIVISION USING BY VALUE argc BY REFERENCE argv.
       Pmain section.
           DISPLAY "N-Queens Problem in COBOL".
           display '  2   4    6     8      10         12           14'.
           display '1 0 0 2 10 4 40 92 352 724 2680 14200 73712 365596'. 

      *    GnuCOBOL command line handling
           ACCEPT argc from ARGUMENT-NUMBER.
           display "argc=", argc.
           if argc >= 1 then
               ACCEPT iend FROM ARGUMENT-VALUE.
               if iend <= 0  or  iend > 14 then
                   move 10 to iend
               end-if.
           if argc >= 2 then
               move iend to istart
               accept iend from argument-value
           end-if.

      *    IBM COBOL command line handling
      *    display "argc=", argc.
      *    if argc > 1 then
      *        set address of argviStr to pargv(2)
      *        move zero to slen
      *        inspect argviStr tallying slen for characters
      *            before initial X"00"
      *        move argviStr(1:slen) to iend
      *        if iend <= 0  or  iend > 14 then
      *            move 10 to iend
      *        end-if.
      *    if argc > 2 then
      *        move iend to istart
      *        set address of argviStr to pargv(3)
      *        move zero to slen
      *        inspect argviStr tallying slen for characters
      *            before initial X"00"
      *        move argviStr(1:slen) to iend
      *    end-if.

           display "istart=", istart, " iend=", iend.
                   
           PERFORM VARYING i FROM istart BY 1 UNTIL i > iend
               perform nqsolve
               DISPLAY "D(", i, ") = ", cnt
           END-PERFORM.

           STOP RUN.



      * Return number of positions for N queens.
       nqsolve section.
           move zero to cnt.
           move 1 to k.
           move 1 to A(1).
           move i to N.
           move i to N2.

       lloop.
           perform configOK.
           if configOKret = 1 then
               if k < N then
                   add 1 to k
                   move 1 to A(k)
                   go to lloop
                else
                   add 1 to cnt
                end-if
           end-if.

           perform with test after varying k from k by -1 until k <= 1
              if A(k) < N then
                  add 1 to A(k)
                  go to lloop
               end-if
           end-perform.

           add 1 to A(1).
           if A(1) > N2 then
               exit section
           end-if.
           move 2 to k.
           move 1 to A(2).
           go to lloop

           exit.



      * Check if k-th queen is attacked by any other prior queen.
      * Return nonzero if configuration is OK, zero otherwise.
       configOK section.
           move zero to configOKret.
           move A(k) to z.

           perform varying j from 1 by 1 until j >= k
               compute l = z - A(j)
               if l = 0 then
                   exit section
               end-if
               if l < 0 then
                   compute l = 0 - l
               end-if
               if l = k - j then
                   exit section
               end-if
           end-perform.

           move 1 to configOKret.
           
           exit.


Added 01-Sep-2024: Referenced in Performance of GnuCOBOL: A free COBOL compiler.