Newsgroup: it.lavoro.informatica
Titolo: Fwd: How do I speed up this code?


Organization: A noiseless patient Spider
Newsgroups: comp.lang.cobol
 
(Originally posted at 
https://sourceforge.net/p/open-cobol/discussion/help/thread/a97718c4/
)
 
I've been working on a RosettaCode challenge. It's Euler's sum of powers 
conjecture. The code below runs but not particularly fast and this is a 
bit of a disappointment because I had assumed that a compiled version 
was going to run a fair bit faster than a VBScript (interpreted) one. As 
it is the COBOL one runs for hours. The VBScript one is done in about a 
minute.
 
I've forgotten a lot of COBOL since I first studied it in 1983. Should I 
forget about using AFTER on the PERFORM VARYING and PERFORM codeblocks 
instead (latest idea)?
 
The VBScript code is
~~~
Option Explicit
Dim max:max=250
 
ReDim P5Table(Max)
Dim i
For i = 0 To Max-1
	P5Table(i) = i * i * i * i * i
Next
 
Dim x0, x1, x2, x3, sum, s1
 
For X0=1 To Max
      For X1=1 To X0
          For X2=1 To X1
              For X3=1 To X2
                  Sum=P5Table(X0)+P5Table(X1)+P5Table(X2)+P5Table(X3)
                  S1=Int(Sum^0.2)
                  If Sum=P5Table(S1) Then
                      WScript.StdOut.Write X0 & " " & X1 & " " & X2 & " 
" & X3 & " " & S1
                      WScript.Quit
                  End If
              Next
          Next
      Next
Next
~~~
and the COBOL
~~~
        ******************************************************************
        * Author: Bruce Axtens
        * Date: 2016-09-02
        * Purpose: 
http://rosettacode.org/wiki/Euler%27s_sum_of_powers_conjecture
 
        * Tectonics: cobc -x -O2 euler1.cbl
        ******************************************************************
         IDENTIFICATION DIVISION.
         PROGRAM-ID. EULER1.
         DATA DIVISION.
         FILE SECTION.
         WORKING-STORAGE SECTION.
         01  PROCESSING-FLAG     PIC 9.
             88  STILL-SEARCHING     VALUE 1.
             88  FINISHED-SEARCHING  VALUE 0.
 
         01  POWER-COUNTER   PIC 999.
             88  POWER-MAX   VALUE 250.
 
         01  WORK.
             03  A               USAGE BINARY-CHAR UNSIGNED.
             03  B               USAGE BINARY-CHAR UNSIGNED.
             03  C               USAGE BINARY-CHAR UNSIGNED.
             03  D               USAGE BINARY-CHAR UNSIGNED.
             03  ABCD            USAGE BINARY-LONG.
             03  FIFTH-ROOT-OFFS USAGE BINARY-CHAR UNSIGNED.
 
         01  PRETTY.
             03  A               PIC ZZ9.
             03  FILLER          VALUE "^5 + ".
             03  B               PIC ZZ9.
             03  FILLER          VALUE "^5 + ".
             03  C               PIC ZZ9.
             03  FILLER          VALUE "^5 + ".
             03  D               PIC ZZ9.
             03  FILLER          VALUE "^5 = ".
             03  FIFTH-ROOT-OFFS PIC ZZ9.
             03  FILLER          VALUE "^5.".
 
         01  FIFTH-POWER-TABLE OCCURS 250 TIMES USAGE BINARY-LONG.
         01  FIFTH-ROOT      USAGE COMPUTATIONAL-1.
 
         PROCEDURE DIVISION.
         MAIN-PARAGRAPH.
             MOVE 1 TO PROCESSING-FLAG.
             PERFORM POWERS-OF-FIVE-TABLE-INIT.
             PERFORM VARYING
                 A IN WORK
                 FROM 1 BY 1 UNTIL A IN WORK = 250
 
                 AFTER B IN WORK
                 FROM 1 BY 1 UNTIL B IN WORK = A IN WORK
 
                 AFTER C IN WORK
                 FROM 1 BY 1 UNTIL C IN WORK = B IN WORK
 
                 AFTER D IN WORK
                 FROM 1 BY 1 UNTIL D IN WORK = C IN WORK
 
                 IF FINISHED-SEARCHING
                      STOP RUN
                 END-IF
 
                 PERFORM POWER-COMPUTATIONS
 
             END-PERFORM.
 
         POWER-COMPUTATIONS.
 
             MOVE ZERO TO ABCD IN WORK.
 
             ADD FIFTH-POWER-TABLE(A IN WORK)
                 FIFTH-POWER-TABLE(B IN WORK)
                 FIFTH-POWER-TABLE(C IN WORK)
                 FIFTH-POWER-TABLE(D IN WORK)
                     TO ABCD IN WORK.
 
             COMPUTE
                 FIFTH-ROOT = ABCD IN WORK ** 0.2.
 
             MOVE FIFTH-ROOT TO FIFTH-ROOT-OFFS IN WORK.
 
             IF FIFTH-POWER-TABLE(FIFTH-ROOT-OFFS IN WORK) =
                     ABCD IN WORK
                 MOVE CORRESPONDING WORK TO PRETTY
                 DISPLAY PRETTY END-DISPLAY
                 MOVE 0 TO PROCESSING-FLAG
             END-IF
             EXIT PARAGRAPH.
 
         POWERS-OF-FIVE-TABLE-INIT.
             PERFORM VARYING POWER-COUNTER FROM 1 BY 1 UNTIL POWER-MAX
                 COMPUTE FIFTH-POWER-TABLE(POWER-COUNTER) =
                     POWER-COUNTER *
                     POWER-COUNTER *
                     POWER-COUNTER *
                     POWER-COUNTER *
                     POWER-COUNTER
                 END-COMPUTE
             END-PERFORM.
             EXIT PARAGRAPH.
 
         END PROGRAM EULER1.
~~~

 By signed numbers, I mean the number definitions, e.g S9(4)
 
For the Micro Focus compiler I use, the default behavior is
TRUNC"ANSI". I did run my posted code with NOTRUNC and it
ran in 70-some seconds. Since COMP-5 behavior is consistent
with NOTRUNC, I used that code for further tweeking.
 
The program now runs in just over 2 seconds.
 
-----
00027 00084 00110 00133 00144
    2.09 sec
-----