f



How do I speed up this code?

(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.
~~~
0
Bruce
9/13/2016 7:34:54 AM
comp.lang.cobol 4278 articles. 1 followers. Post Follow

11 Replies
411 Views

Similar Articles

[PageSpeed] 59

In article <nr8a6u$7o5$1@dont-email.me>,
Bruce Axtens  <bruce.axtens@gmail.com> wrote:
>(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.

'Gosh, this torque-wrench doesn't work very well as a hammer!'

Euler's sum of powers conjectures isn't, to the best of my knowledge, 
oriented to many common businesses.

DD
0
docdwarf
9/13/2016 12:11:52 PM
On Tuesday, 13 September 2016 20:11:53 UTC+8, docd...@panix.com  wrote:
> Euler's sum of powers conjectures isn't, to the best of my knowledge, 
> oriented to many common businesses.

Quite true. However, here I am trying anyway. Should I not ask these kind of questions in comp.lang.cobol? 
0
Bruce
9/13/2016 2:15:02 PM
On 9/13/2016 10:15 AM, Bruce Axtens wrote:
> On Tuesday, 13 September 2016 20:11:53 UTC+8, docd...@panix.com  wrote:
>> Euler's sum of powers conjectures isn't, to the best of my knowledge,
>> oriented to many common businesses.
>
> Quite true. However, here I am trying anyway. Should I not ask these kind of questions in comp.lang.cobol?
>

Of course you should... just beware of the Doc(dwarf).
  :)


0
Kerry
9/13/2016 2:23:18 PM
In article <b095d1f6-3821-4ace-83da-cf2cbb755c38@googlegroups.com>,
Bruce Axtens  <bruce.axtens@gmail.com> wrote:
>On Tuesday, 13 September 2016 20:11:53 UTC+8, docd...@panix.com  wrote:
>> Euler's sum of powers conjectures isn't, to the best of my knowledge, 
>> oriented to many common businesses.
>
>Quite true. However, here I am trying anyway. Should I not ask these
>kind of questions in comp.lang.cobol? 

I've had some Very Good Time result from doing things I was told I 
shouldn't.

DD

0
docdwarf
9/13/2016 2:49:27 PM
On Tuesday, September 13, 2016 at 3:34:59 AM UTC-4, Bruce Axtens wrote:
> (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)?

[snip VBScript]

> 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.

POWER-COUNTER is used in calculations. The default USAGE DISPLAY
is inefficient.

>         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.

USAGE BINARY-CHAR is less efficient than a usage that matches
the word size of the processor, whether 16-, 32-, or 64-bits

>             03  ABCD            USAGE BINARY-LONG.

The maximum value stored in ABCD is 3906250000000 (13 digits)
BINARY-LONG is only good for 9 digits.

>             03  FIFTH-ROOT-OFFS USAGE BINARY-CHAR UNSIGNED.

USAGE BINARY-CHAR is less efficient than a usage that matches
the word size of the processor, whether 16-, 32-, or 64-bits

>         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.

The maximum value stored in this table is 976562500000 (12 digits)
BINARY-LONG is only good for 9 digits.

>         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

I see the definition, but I don't see any references to
FINISHED-SEARCHING. How could the program terminate gracefully?

>                 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.

Floating-point truncation may give an erroneous result
in calculating the table offset!

>             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

The COBOL standard provides explicit requirements for calculating
powers. POWER-COUNTER ** 5 may be more efficient.

>                 END-COMPUTE
>             END-PERFORM.
>             EXIT PARAGRAPH.
> 
>         END PROGRAM EULER1.
> ~~~

While I have posted COBOL to Rosetta Code, this is not a task
for which I would post code. Nonetheless, in an effort to
understand the task and find potential problems with your code,
I proceeded to write a solution using standard COBOL 85.

-----
       identification division.
       program-id. euler.
       data division.
       working-storage section.
       1 binary.
        2 sum-of-4 pic 9(18).
        2 x0 pic 9(4).
        2 x1 pic 9(4).
        2 x2 pic 9(4).
        2 x3 pic 9(4).
        2 y pic 9(4).
        2 i pic 9(4).
       1 binary.
        2 t-able occurs 250 ascending t indexed by t-idx.
         3 t pic 9(18).
       1 end-time.
        2 end-hh pic 99.
        2 end-mm pic 99.
        2 end-ss pic 99v99.
       1 start-time.
        2 start-hh pic 99.
        2 start-mm pic 99.
        2 start-ss pic 99v99.
       1 elapsed-time.
        2 elapsed-ss pic zzz9.99.
       procedure division.
       begin.
           accept start-time from time
           perform varying i from 1 by 1
           until i > 250
               compute t (i) = i ** 5
           end-perform
           perform varying x0 from 1 by 1
           until x0 > 250
           add 1 x0 giving x1
           perform varying x1 from x1 by 1
           until x1 > 250
           add 1 x1 giving x2
           perform varying x2 from x2 by 1
           until x2 > 250
           add 1 x2 giving x3
           perform varying x3 from x3 by 1
           until x3 > 250
               compute sum-of-4 =
                   t (x0) + t (x1) + t (x2) + t (x3)
               if sum-of-4 > t (250)
                   continue
               else
                   search all t-able
                   when t (t-idx) = sum-of-4
                       set y to t-idx
                       perform display-result
                   end-search
               end-if
           end-perform
           end-perform
           end-perform
           end-perform
           accept end-time from time
           perform compute-elapsed
           stop run
           .

       display-result.
           display x0 space x1 space x2 space x3 space y
           .

       compute-elapsed.
           compute elapsed-ss =
               ((end-hh * 3600) + (end-mm * 60) + end-ss)
             - ((start-hh * 3600) + (start-mm * 60) + start-ss)
           display elapsed-ss space "sec"
           .
       end program euler.
-----
Output:

0027 0084 0110 0133 0144
  84.09 sec
-----

When I replaced binary with non-standard comp-5

       replace ==binary== by ==comp-5==.

the output was:

00027 00084 00110 00133 00144
  20.65 sec
-----

Feel free to work from this code, if you like.
0
Rick
9/13/2016 6:03:48 PM
On 14/09/2016 2:03 AM, Rick Smith wrote:
> Feel free to work from this code, if you like.
Thank you very much indeed for the thorough critique of the code! Very 
much appreciated.

Bruce/bugmagnet
0
Bruce
9/14/2016 2:24:27 AM
On Tuesday, 13 September 2016 19:03:51 UTC+1, Rick Smith  wrote:
> On Tuesday, September 13, 2016 at 3:34:59 AM UTC-4, Bruce Axtens wrote:
> > (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)?
> 
> [snip VBScript]
> 
> > 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.
> 
> POWER-COUNTER is used in calculations. The default USAGE DISPLAY
> is inefficient.
> 
> >         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.
> 
> USAGE BINARY-CHAR is less efficient than a usage that matches
> the word size of the processor, whether 16-, 32-, or 64-bits
> 
> >             03  ABCD            USAGE BINARY-LONG.
> 
> The maximum value stored in ABCD is 3906250000000 (13 digits)
> BINARY-LONG is only good for 9 digits.
> 
> >             03  FIFTH-ROOT-OFFS USAGE BINARY-CHAR UNSIGNED.
> 
> USAGE BINARY-CHAR is less efficient than a usage that matches
> the word size of the processor, whether 16-, 32-, or 64-bits
> 
> >         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.
> 
> The maximum value stored in this table is 976562500000 (12 digits)
> BINARY-LONG is only good for 9 digits.
> 
> >         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
> 
> I see the definition, but I don't see any references to
> FINISHED-SEARCHING. How could the program terminate gracefully?
> 
> >                 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.
> 
> Floating-point truncation may give an erroneous result
> in calculating the table offset!
> 
> >             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
> 
> The COBOL standard provides explicit requirements for calculating
> powers. POWER-COUNTER ** 5 may be more efficient.
> 
> >                 END-COMPUTE
> >             END-PERFORM.
> >             EXIT PARAGRAPH.
> > 
> >         END PROGRAM EULER1.
> > ~~~
> 
> While I have posted COBOL to Rosetta Code, this is not a task
> for which I would post code. Nonetheless, in an effort to
> understand the task and find potential problems with your code,
> I proceeded to write a solution using standard COBOL 85.
> 
> -----
>        identification division.
>        program-id. euler.
>        data division.
>        working-storage section.
>        1 binary.
>         2 sum-of-4 pic 9(18).
>         2 x0 pic 9(4).
>         2 x1 pic 9(4).
>         2 x2 pic 9(4).
>         2 x3 pic 9(4).
>         2 y pic 9(4).
>         2 i pic 9(4).
>        1 binary.
>         2 t-able occurs 250 ascending t indexed by t-idx.
>          3 t pic 9(18).
>        1 end-time.
>         2 end-hh pic 99.
>         2 end-mm pic 99.
>         2 end-ss pic 99v99.
>        1 start-time.
>         2 start-hh pic 99.
>         2 start-mm pic 99.
>         2 start-ss pic 99v99.
>        1 elapsed-time.
>         2 elapsed-ss pic zzz9.99.
>        procedure division.
>        begin.
>            accept start-time from time
>            perform varying i from 1 by 1
>            until i > 250
>                compute t (i) = i ** 5
>            end-perform
>            perform varying x0 from 1 by 1
>            until x0 > 250
>            add 1 x0 giving x1
>            perform varying x1 from x1 by 1
>            until x1 > 250
>            add 1 x1 giving x2
>            perform varying x2 from x2 by 1
>            until x2 > 250
>            add 1 x2 giving x3
>            perform varying x3 from x3 by 1
>            until x3 > 250
>                compute sum-of-4 =
>                    t (x0) + t (x1) + t (x2) + t (x3)
>                if sum-of-4 > t (250)
>                    continue
>                else
>                    search all t-able
>                    when t (t-idx) = sum-of-4
>                        set y to t-idx
>                        perform display-result
>                    end-search
>                end-if
>            end-perform
>            end-perform
>            end-perform
>            end-perform
>            accept end-time from time
>            perform compute-elapsed
>            stop run
>            .
> 
>        display-result.
>            display x0 space x1 space x2 space x3 space y
>            .
> 
>        compute-elapsed.
>            compute elapsed-ss =
>                ((end-hh * 3600) + (end-mm * 60) + end-ss)
>              - ((start-hh * 3600) + (start-mm * 60) + start-ss)
>            display elapsed-ss space "sec"
>            .
>        end program euler.
> -----
> Output:
> 
> 0027 0084 0110 0133 0144
>   84.09 sec
> -----
> 
> When I replaced binary with non-standard comp-5
> 
>        replace ==binary== by ==comp-5==.
> 
> the output was:
> 
> 00027 00084 00110 00133 00144
>   20.65 sec
> -----
> 
> Feel free to work from this code, if you like.

That's a great solution.  However, it used to be the case on IBM systems that using signed numbers for counts and in tests was faster, because the compiler didn't have to strip the signs to create the results of calculations.

Robert
0
0robert
9/15/2016 8:06:57 PM
On Thursday, 15 September 2016 21:06:59 UTC+1, 0rober...@gmail.com  wrote:
> On Tuesday, 13 September 2016 19:03:51 UTC+1, Rick Smith  wrote:
> > On Tuesday, September 13, 2016 at 3:34:59 AM UTC-4, Bruce Axtens wrote:
> > > (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)?
> > 
> > [snip VBScript]
> > 
> > > 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.
> > 
> > POWER-COUNTER is used in calculations. The default USAGE DISPLAY
> > is inefficient.
> > 
> > >         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.
> > 
> > USAGE BINARY-CHAR is less efficient than a usage that matches
> > the word size of the processor, whether 16-, 32-, or 64-bits
> > 
> > >             03  ABCD            USAGE BINARY-LONG.
> > 
> > The maximum value stored in ABCD is 3906250000000 (13 digits)
> > BINARY-LONG is only good for 9 digits.
> > 
> > >             03  FIFTH-ROOT-OFFS USAGE BINARY-CHAR UNSIGNED.
> > 
> > USAGE BINARY-CHAR is less efficient than a usage that matches
> > the word size of the processor, whether 16-, 32-, or 64-bits
> > 
> > >         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.
> > 
> > The maximum value stored in this table is 976562500000 (12 digits)
> > BINARY-LONG is only good for 9 digits.
> > 
> > >         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
> > 
> > I see the definition, but I don't see any references to
> > FINISHED-SEARCHING. How could the program terminate gracefully?
> > 
> > >                 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.
> > 
> > Floating-point truncation may give an erroneous result
> > in calculating the table offset!
> > 
> > >             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
> > 
> > The COBOL standard provides explicit requirements for calculating
> > powers. POWER-COUNTER ** 5 may be more efficient.
> > 
> > >                 END-COMPUTE
> > >             END-PERFORM.
> > >             EXIT PARAGRAPH.
> > > 
> > >         END PROGRAM EULER1.
> > > ~~~
> > 
> > While I have posted COBOL to Rosetta Code, this is not a task
> > for which I would post code. Nonetheless, in an effort to
> > understand the task and find potential problems with your code,
> > I proceeded to write a solution using standard COBOL 85.
> > 
> > -----
> >        identification division.
> >        program-id. euler.
> >        data division.
> >        working-storage section.
> >        1 binary.
> >         2 sum-of-4 pic 9(18).
> >         2 x0 pic 9(4).
> >         2 x1 pic 9(4).
> >         2 x2 pic 9(4).
> >         2 x3 pic 9(4).
> >         2 y pic 9(4).
> >         2 i pic 9(4).
> >        1 binary.
> >         2 t-able occurs 250 ascending t indexed by t-idx.
> >          3 t pic 9(18).
> >        1 end-time.
> >         2 end-hh pic 99.
> >         2 end-mm pic 99.
> >         2 end-ss pic 99v99.
> >        1 start-time.
> >         2 start-hh pic 99.
> >         2 start-mm pic 99.
> >         2 start-ss pic 99v99.
> >        1 elapsed-time.
> >         2 elapsed-ss pic zzz9.99.
> >        procedure division.
> >        begin.
> >            accept start-time from time
> >            perform varying i from 1 by 1
> >            until i > 250
> >                compute t (i) = i ** 5
> >            end-perform
> >            perform varying x0 from 1 by 1
> >            until x0 > 250
> >            add 1 x0 giving x1
> >            perform varying x1 from x1 by 1
> >            until x1 > 250
> >            add 1 x1 giving x2
> >            perform varying x2 from x2 by 1
> >            until x2 > 250
> >            add 1 x2 giving x3
> >            perform varying x3 from x3 by 1
> >            until x3 > 250
> >                compute sum-of-4 =
> >                    t (x0) + t (x1) + t (x2) + t (x3)
> >                if sum-of-4 > t (250)
> >                    continue
> >                else
> >                    search all t-able
> >                    when t (t-idx) = sum-of-4
> >                        set y to t-idx
> >                        perform display-result
> >                    end-search
> >                end-if
> >            end-perform
> >            end-perform
> >            end-perform
> >            end-perform
> >            accept end-time from time
> >            perform compute-elapsed
> >            stop run
> >            .
> > 
> >        display-result.
> >            display x0 space x1 space x2 space x3 space y
> >            .
> > 
> >        compute-elapsed.
> >            compute elapsed-ss =
> >                ((end-hh * 3600) + (end-mm * 60) + end-ss)
> >              - ((start-hh * 3600) + (start-mm * 60) + start-ss)
> >            display elapsed-ss space "sec"
> >            .
> >        end program euler.
> > -----
> > Output:
> > 
> > 0027 0084 0110 0133 0144
> >   84.09 sec
> > -----
> > 
> > When I replaced binary with non-standard comp-5
> > 
> >        replace ==binary== by ==comp-5==.
> > 
> > the output was:
> > 
> > 00027 00084 00110 00133 00144
> >   20.65 sec
> > -----
> > 
> > Feel free to work from this code, if you like.
> 
> That's a great solution.  However, it used to be the case on IBM systems that using signed numbers for counts and in tests was faster, because the compiler didn't have to strip the signs to create the results of calculations.
> 
> Robert

By signed numbers, I mean the number definitions, e.g S9(4)
0
0robert
9/15/2016 8:08:34 PM
On Thursday, September 15, 2016 at 4:08:40 PM UTC-4, 0rober...@gmail.com wrote:
> On Thursday, 15 September 2016 21:06:59 UTC+1, 0rober...@gmail.com  wrote:
> > On Tuesday, 13 September 2016 19:03:51 UTC+1, Rick Smith  wrote:

[snip]

> > > [...] I proceeded to write a solution using standard COBOL 85.
> > > 
> > > -----

[snip]

> > >        working-storage section.
> > >        1 binary.
> > >         2 sum-of-4 pic 9(18).
> > >         2 x0 pic 9(4).
> > >         2 x1 pic 9(4).
> > >         2 x2 pic 9(4).
> > >         2 x3 pic 9(4).
> > >         2 y pic 9(4).
> > >         2 i pic 9(4).
> > >        1 binary.
> > >         2 t-able occurs 250 ascending t indexed by t-idx.
> > >          3 t pic 9(18).

[smip]

> > > -----
> > > Output:
> > > 
> > > 0027 0084 0110 0133 0144
> > >   84.09 sec
> > > -----
> > > 
> > > When I replaced binary with non-standard comp-5
> > > 
> > >        replace ==binary== by ==comp-5==.
> > > 
> > > the output was:
> > > 
> > > 00027 00084 00110 00133 00144
> > >   20.65 sec
> > > -----
> > > 
> > > Feel free to work from this code, if you like.
> > 
> > That's a great solution.  However, it used to be the case on IBM systems that using signed numbers for counts and in tests was faster, because the compiler didn't have to strip the signs to create the results of calculations.
> > 
> 
> 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 the original code with NOTRUNC and it
ran in 70-some seconds. Since COMP-5
0
Rick
9/16/2016 4:06:17 AM
On Thursday, September 15, 2016 at 4:08:40 PM UTC-4, 0rober...@gmail.com wrote:
> On Thursday, 15 September 2016 21:06:59 UTC+1, 0rober...@gmail.com  wrote:
> > On Tuesday, 13 September 2016 19:03:51 UTC+1, Rick Smith  wrote:

[snip]

> > > [...] I proceeded to write a solution using standard COBOL 85.
> > > 
> > > -----

[snip]

> > >        working-storage section.
> > >        1 binary.
> > >         2 sum-of-4 pic 9(18).
> > >         2 x0 pic 9(4).
> > >         2 x1 pic 9(4).
> > >         2 x2 pic 9(4).
> > >         2 x3 pic 9(4).
> > >         2 y pic 9(4).
> > >         2 i pic 9(4).
> > >        1 binary.
> > >         2 t-able occurs 250 ascending t indexed by t-idx.
> > >          3 t pic 9(18).

[snip]

> > > -----
> > > Output:
> > > 
> > > 0027 0084 0110 0133 0144
> > >   84.09 sec
> > > -----
> > > 
> > > When I replaced binary with non-standard comp-5
> > > 
> > >        replace ==binary== by ==comp-5==.
> > > 
> > > the output was:
> > > 
> > > 00027 00084 00110 00133 00144
> > >   20.65 sec
> > > -----
> > > 
> > > Feel free to work from this code, if you like.
> > 
> > That's a great solution.  However, it used to be the case on IBM systems that using signed numbers for counts and in tests was faster, because the compiler didn't have to strip the signs to create the results of calculations.
> > 
> 
> 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
-----
0
Rick
9/16/2016 4:14:45 AM
docdwarf@panix.com () writes:

> In article <nr8a6u$7o5$1@dont-email.me>,
> Bruce Axtens  <bruce.axtens@gmail.com> wrote:
>>(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.
>
> 'Gosh, this torque-wrench doesn't work very well as a hammer!'
>
> Euler's sum of powers conjectures isn't, to the best of my knowledge, 
> oriented to many common businesses.

Neither is FizzBuzz, this doesn't prevent java programmer to attempt it
nonetheless:
https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition
:-)

-- 
__Pascal Bourguignon__

Croire en l'histoire officielle, c'est croire des criminels sur parole.
-- Simone WEIL (1909-1943) philosophe Fran´┐Żaise.
0
Pascal
10/2/2016 12:07:22 PM
Reply: