Subscript vs Index in COBOL



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Subscript vs Index in COBOL

Postby Lightning Lad » Thu Jan 21, 2010 8:32 pm

Hi,

In a recent performance drive it was a suggested that usage of indexes was better than subscripts and would lead to performance improvement in a COBOL code.

EXISTING CODE:

01 H-VAR.
03 H-VAR1 PIC X(5) OCCURS 10.

If index is used in the above code will this lead to any significant performance improvement in COBOL?
Lightning Lad
 
Posts: 5
Joined: Thu Jan 21, 2010 8:13 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Subscripts vs. Indexes

Postby Robert Sample » Thu Jan 21, 2010 8:39 pm

Indexes are more efficient than subscripts since they represent an offset into the array directly rather than having to be converted from an occurrence number to an offset. Having said that, though, unless you are accessing the array tens (or hundreds) of millions of times in your code you will see no appreciable difference in performance between indexes and subscripts.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Subscripts vs. Indexes

Postby chaat » Sat Jan 30, 2010 10:44 am

from the perspective of an employer, lets assume that you need a program written which makes very heavy use of array processing.

You have two programmers to choose between.

programmer A writes inefficient code using subscripts which cost $100,000.00 per year in operational cpu costs.

programmer B writes efficient code using subscripts which cost $30,000.00 per year in operational cpu costs.


Which programmer would you hire ?

Chuck Haatvedt

From my own perspective I always try to deliver a cost effective solution to my employer. As a person learns how to code efficiently, it doesn't take much additional effort to write efficient code.
chaat
 
Posts: 27
Joined: Sun Aug 16, 2009 11:07 pm
Location: St. Cloud, Minnesota
Has thanked: 0 time
Been thanked: 1 time

Re: Subscripts vs. Indexes

Postby dick scherrer » Sat Jan 30, 2010 10:52 am

Hello,

I'd hire the person who wrote code that worked 100% of the time and was easily maintainable.

I've not yet hired anyone because i believed they would write more efficient array-handling code.

A very small percent of application code is "array processing bound". . .
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Subscripts vs. Indexes

Postby gwaj1949 » Fri Feb 05, 2010 6:36 am

I saw this topic while trying to figure out the following results while doing some benchmarking for a client.

We are attempting to prove the IBM position that using indexes to access tables is the fastest, using subscripts of usage COMP is 30% slower, using subscripts of usage COMP-3 is 200% slower and using subscripts of usage DISPLAY is 450% slower (see IBM reference at end of post).

The table size is either 10000 or 50000 rows. There are 4 fields per row. The program loops through the following 1000 times: initialize table, load the entire table, unload the entire table to a report (dummied out, of course). The following results show the CPU seconds for each execution.

TABLE
SIZE DISPLAY COMP COMP-3 INDEXED

10000 2.80 2.31 2.79 2.28
50000 13.40 10.67 12.13 10.74

These results show that it really doesn't matter what usage you assign to the subscript, the biggest savings (COMP vs. DISPLAY) is only 25.6%. Also, indexes are basically the same as usage COMP. Can anyone explain these results?

IBM Enterprise COBOL Version 3 Release 1
Performance Tuning
January 16, 2002
R. J. Arellanes
IBM Corporation
Software Solutions
555 Bailey Avenue
San Jose, CA 95141

Indexes vs Subscripts
Using indexes to address a table is more efficient than using subscripts since the index already contains the
displacement from the start of the table and does not have to be calculated at run time. Subscripts, on the
other hand, contain an occurrence number that must be converted to a displacement value at run time
before it can be used. When using subscripts to address a table, use a binary (COMP) signed data item with
eight or fewer digits (for example, using PICTURE S9(8) COMP for the data item). This will allow fullword
arithmetic to be used during the calculations. Additionally, in some cases, using four or fewer digits for the
data item may also offer some added reduction in CPU time since halfword arithmetic can be used.
| Performance considerations for indexes vs subscripts (PIC S9(8)):
| using binary data items (COMP) to address a table is 30% slower than using indexes
| using packed decimal data items (COMP-3) to address a table is 300% slower than using indexes
| using DISPLAY data items to address a table is 450% slower than using indexes
| (COB PG: pp 59-61, 537-538)
gwaj1949
 
Posts: 1
Joined: Fri Feb 05, 2010 6:22 am
Has thanked: 0 time
Been thanked: 0 time

Re: Subscripts vs. Indexes

Postby dick scherrer » Fri Feb 05, 2010 8:21 am

Hello and welcome to the forum,

You would need to know exactly what IBM used to perform the data gathering. The best (and maybe only) place to get this information is from the people who ran and published the information.

Notice that the quoted material is 8 years old (probably more as it takes time to get everything ready for publication).
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: Subscripts vs. Indexes

Postby Robert Sample » Fri Feb 05, 2010 6:28 pm

The timings IBM refers to are probably just the memory access, while you are measuring the entire program. This is much like comparing apples to elephants -- not even in the same kingdom, much less anything closer. If you really think you need to replicate IBM's results you would have to have (probably several weeks of) intensive discussions with IBM about their methodology, ensure you have tools to measure individual machine instructions (very expensive, I'm sure), and spend a few weeks developing the precise code needed for the testing -- just throwing together a program is probably the worst possible way to do this type of testing.

The bigger question, however, is why on earth would you think you need to replicate IBM's results? They've done the work, published it, and you can therefore take advantage of it without having to do it yourself. Doing so is an expensive, and probably futile, effort.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Subscripts vs. Indexes

Postby chaat » Mon Feb 08, 2010 7:23 am

When doing an index vs subscript performance test, I use the the following to test relative performance.

In the examples below look at the "UNTIL" clause in the TEST009 program. When comparing an index to a numeric field, the compiler generates extra code to convert both to the same format. Remember than an INDEX is stored as a displacement so when comparing to a number the index has to be divided by the length of 1 entry of the array. Then it has to add 1 to that number. The logic to convert the index to a numeric value would distort the comparison.

Note that TEST009 uses an elementary USAGE INDEX field to eliminate this conversion every time thru the loop. Use the "LIST" compiler option and look at the generated ASSEMBLER code for the UNTIL clause.

a comparison which will show the difference is between TEST005 and TEST009 as they both use INDEXES. The only difference is the UNTIL clause.

       IDENTIFICATION DIVISION.
         PROGRAM-ID. TEST001.
         AUTHOR.     CHUCK HAATVEDT
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  SUB-1                           PIC S9(9)   COMP.
       01  SUB-2                           PIC S9(9)   COMP.

       01  TBL.
           05  TBL-ENTRY-LVL-1 OCCURS 100000 TIMES
                               INDEXED BY TBL-1-INDEX.
               10  TBL-ENTRY-LVL-2 OCCURS 10 TIMES
                                   INDEXED BY TBL-2-INDEX.
                   15  TBL-TEXT-1          PIC X.
                   15  TBL-CHAR-1          PIC X.

       PROCEDURE DIVISION.

           PERFORM 050 TIMES
               PERFORM VARYING SUB-1 FROM 1 BY 1
                   UNTIL SUB-1 > 100000
                   PERFORM VARYING SUB-2 FROM 1 BY 1
                       UNTIL SUB-2 > 10
                           MOVE SPACES TO TBL-TEXT-1 (SUB-1, SUB-2)
                           MOVE 'A'    TO TBL-CHAR-1 (SUB-1, SUB-2)
                   END-PERFORM
               END-PERFORM
           END-PERFORM.

           GOBACK.


       IDENTIFICATION DIVISION.
         PROGRAM-ID. TEST002.
         AUTHOR.     CHUCK HAATVEDT
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  SUB-1                           PIC S9(9)   COMP-3.
       01  SUB-2                           PIC S9(9)   COMP-3.

       01  TBL.
           05  TBL-ENTRY-LVL-1 OCCURS 100000 TIMES
                               INDEXED BY TBL-1-INDEX.
               10  TBL-ENTRY-LVL-2 OCCURS 10 TIMES
                                   INDEXED BY TBL-2-INDEX.
                   15  TBL-TEXT-1          PIC X.
                   15  TBL-CHAR-1          PIC X.

       PROCEDURE DIVISION.

           PERFORM 050 TIMES
               PERFORM VARYING SUB-1 FROM 1 BY 1
                   UNTIL SUB-1 > 100000
                   PERFORM VARYING SUB-2 FROM 1 BY 1
                       UNTIL SUB-2 > 10
                           MOVE SPACES TO TBL-TEXT-1 (SUB-1, SUB-2)
                           MOVE 'A'    TO TBL-CHAR-1 (SUB-1, SUB-2)
                   END-PERFORM
               END-PERFORM
           END-PERFORM.

           GOBACK.


       IDENTIFICATION DIVISION.
         PROGRAM-ID. TEST003.
         AUTHOR.     CHUCK HAATVEDT
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  SUB-1                           PIC 9(9).
       01  SUB-2                           PIC 9(9).

       01  TBL.
           05  TBL-ENTRY-LVL-1 OCCURS 100000 TIMES
                               INDEXED BY TBL-1-INDEX.
               10  TBL-ENTRY-LVL-2 OCCURS 10 TIMES
                                   INDEXED BY TBL-2-INDEX.
                   15  TBL-TEXT-1          PIC X.
                   15  TBL-CHAR-1          PIC X.

       PROCEDURE DIVISION.

           PERFORM 050 TIMES
               PERFORM VARYING SUB-1 FROM 1 BY 1
                   UNTIL SUB-1 > 100000
                   PERFORM VARYING SUB-2 FROM 1 BY 1
                       UNTIL SUB-2 > 10
                           MOVE SPACES TO TBL-TEXT-1 (SUB-1, SUB-2)
                           MOVE 'A'    TO TBL-CHAR-1 (SUB-1, SUB-2)
                   END-PERFORM
               END-PERFORM
           END-PERFORM.

           GOBACK.


       IDENTIFICATION DIVISION.
         PROGRAM-ID. TEST005.
         AUTHOR.     CHUCK HAATVEDT
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  MAX-1                           PIC S9(9).
       01  MAX-2                           PIC S9(9).
       01  WS-1                            PIC 9       VALUE 1.

       01  TBL.
           05  TBL-ENTRY-LVL-1 OCCURS 100000 TIMES
                               INDEXED BY TBL-1-INDEX.
               10  TBL-ENTRY-LVL-2 OCCURS 10 TIMES
                                   INDEXED BY TBL-2-INDEX.
                   15  TBL-TEXT-1          PIC X.
                   15  TBL-CHAR-1          PIC X.

       PROCEDURE DIVISION.

           MOVE +100000                TO MAX-1.
           MOVE +10                    TO MAX-2.

           PERFORM 050 TIMES
               PERFORM VARYING TBL-1-INDEX FROM WS-1 BY WS-1
                   UNTIL TBL-1-INDEX > MAX-1
                   PERFORM VARYING TBL-2-INDEX FROM WS-1 BY WS-1
                       UNTIL TBL-2-INDEX > MAX-2
                           MOVE SPACES
                               TO TBL-TEXT-1 (TBL-1-INDEX, TBL-2-INDEX)
                           MOVE 'A'
                               TO TBL-CHAR-1 (TBL-1-INDEX, TBL-2-INDEX)
                   END-PERFORM
               END-PERFORM
           END-PERFORM.

           GOBACK.


       
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TEST009.
       AUTHOR.     CHUCK HAATVEDT
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01  MAX-1                           PIC S9(9) COMP.
       01  MAX-2                           PIC S9(9) COMP.

       01  SVIDX-SAVE-INDEXES.
           05  SVIDX-TBL-1-INDEX           USAGE INDEX.
           05  SVIDX-TBL-2-INDEX           USAGE INDEX.
           
       01  TBL.
           05  TBL-ENTRY-LVL-1 OCCURS 100000 TIMES
                               INDEXED BY TBL-1-INDEX
                                          TBL-1-INDEX-MAX.
               10  TBL-ENTRY-LVL-2 OCCURS 10 TIMES
                                   INDEXED BY TBL-2-INDEX
                                              TBL-2-INDEX-MAX.
                   15  TBL-TEXT-1          PIC X.
                   15  TBL-CHAR-1          PIC X.

       PROCEDURE DIVISION.

           MOVE +100000                TO MAX-1.
           MOVE +10                       TO MAX-2.
           SET TBL-1-INDEX             TO MAX-1.
           SET SVIDX-TBL-1-INDEX   TO TBL-1-INDEX.
           SET TBL-2-INDEX             TO MAX-2.
           SET SVIDX-TBL-2-INDEX    TO TBL-2-INDEX.

           PERFORM 050 TIMES
               SET TBL-1-INDEX         TO +1
               PERFORM WITH TEST BEFORE
                   UNTIL TBL-1-INDEX > SVIDX-TBL-1-INDEX
                   SET TBL-2-INDEX     TO +1
                   PERFORM WITH TEST BEFORE
                       UNTIL TBL-2-INDEX > SVIDX-TBL-2-INDEX
                           MOVE SPACES
                               TO TBL-TEXT-1 (TBL-1-INDEX, TBL-2-INDEX)
                           MOVE 'A'
                               TO TBL-CHAR-1 (TBL-1-INDEX, TBL-2-INDEX)
                           SET TBL-2-INDEX     UP BY +1
                   END-PERFORM
                   SET TBL-1-INDEX             UP BY +1
               END-PERFORM
           END-PERFORM.

           GOBACK.
chaat
 
Posts: 27
Joined: Sun Aug 16, 2009 11:07 pm
Location: St. Cloud, Minnesota
Has thanked: 0 time
Been thanked: 1 time

Re: Subscripts vs. Indexes

Postby kssuraj » Fri Mar 05, 2010 10:54 pm

Hi Guys,

I and my team member had a argument on subscript.I said " value of subscript can be zero"..When set to zero, cobol implicitly assumes it lowest value as one.Please let me know your thoughts whether I am correct or wrong???
kssuraj
 
Posts: 1
Joined: Fri Mar 05, 2010 10:42 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Subscripts vs. Indexes

Postby dick scherrer » Sat Mar 06, 2010 1:08 am

Hello and welcome to the forum,

Suggest you run a little experiment on your system and if you see something that is confusing, post your doubt back here - someone will be able to clarify.

When set to zero, cobol implicitly assumes it lowest value as one
Please post which documentation has this. . .
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post